Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No option to upload directory #529

Closed
jeysaravana opened this issue Feb 28, 2020 · 5 comments
Closed

No option to upload directory #529

jeysaravana opened this issue Feb 28, 2020 · 5 comments

Comments

@jeysaravana
Copy link

there is no option to use your plugin to uploadDirectory by using as3cf global object. It will be nice to include UploadDirectory option or if we can get use of S3Client object
wp-amazon-s3-and-cloudfront/classes/providers/aws-provider.php

@ianmjones
Copy link
Member

Could you expand on the use case?

@jeysaravana
Copy link
Author

jeysaravana commented Mar 11, 2020

Hi @ianmjones ,
thanks for looking into this. I used Amazon Web Services plugins and WP Offload Media pro to connect my site to AWS. after I updated WP Offload Media from WP Offload S3, I got to know that I don't need that Amazon Web Services plugin.
My Scenario is this. I tried to upload entire directory to AWS and accessible to public.
I assign client like this

global $as3cf;
$s3_config            = new stdClass();
$s3_config->client    = $as3cf->get_provider_client( $s3_region, true );
$s3_config->s3_bucket = $s3_bucket;
$s3_config->scheme    = $as3cf->get_url_scheme();
$s3_config->domain    = $as3cf->get_provider()->get_url_domain( $s3_config->s3_bucket, '', null, array(), true );
$s3_config->endpoint  = trailingslashit( $s3_config->scheme . '://' . $s3_config->domain );


// listing objects
$s3_client    = $s3_config->client;
$list_objects = $s3_client->list_objects(
	array(
		'Bucket' => $s3_bucket,
		'Prefix' => 'wp-content/uploads/id123',
	)
);

if ( is_array( $list_objects ) && ! empty( $list_objects['Contents'] ) ) {

	$keys        = $list_objects['Contents'];
	$keys_mapped = array_map(
		function ( $key ) {
			return array(
				'Key' => $key['Key'],
			);
		},
		$keys
	);

	// check if the keys exist before trying to delete the files.
	if ( ! empty( $keys ) ) {
		// Delete the objects from s3.
		$result = $s3_client->delete_objects(
			array(
				'Bucket'  => $s3_bucket,
				'Objects' => $keys_mapped,
			)
		);
	}
}


// Uploading individual file - this works
$full_path = __DIR__ . '/uploads/file.jpg';
$s3_upload_args = array(
	'Bucket'     => $s3_bucket,
	'Key'        => 'wp-content/uploads/id123',
	'SourceFile' => $full_path,
	'ACL'        => 'public-read',
));
);
$s3_client->upload_object( $s3_upload_args );





// Uploading directory --- NOT WORKING 
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\S3\S3Client;

$source_path = __DIR__ . '/uploads/';
$upload_path = 'wp-content/uploads/directory';
$s3_upload_options = array(
	'params' => array(
		'ACL' => 'public-read',
	),
);

// I couldn't use as3cf global object because it doesn't have upload directory method.
// instead I had to use S3Client directly. But it is not using the ACL as public-read
// it throws permission issue when I access it through public url.
$s3 = new S3Client(
	array(
		'version' => 'latest',
		'region'  => 'us-east-1',
	)
);

$s3->uploadDirectory( $source_path, $s3_config->s3_bucket, $upload_path, $s3_upload_options );

It will be great to include upload directory option in the libraries. You are using own namespace to the library files. because of that, I couldn't get those thing works.
this https://github.com/deliciousbrains/wp-amazon-s3-and-cloudfront/blob/master/vendor/Aws3/Aws/S3/S3ClientInterface.php#L197
and https://github.com/deliciousbrains/wp-amazon-web-services/blob/master/vendor/aws/Aws/S3/S3Client.php#L567 are different methods.

@ianmjones
Copy link
Member

Ok, so you're basically wanting a way to upload to S3 a bunch of files from a directory that aren't in the Media Library, correct?

If so, isn't this a duplicate of #7 ?

WP Offload Media currently only supports offloading and rewriting URLs for Media Library items, but we do plan to support offloading local media that isn't registered in the Media Library.

We also plan to have a proper API that allows third party developers get info about an offloaded item, whether that be a Media Library item or not.

At the moment there's a single as3cf_get_secure_attachment_url function available, but we're going to slowly start expanding that, with what is yet to be decided.

But it should be emphasized that WP Offload Media is not a successor to the Amazon Web Services plugin, that was a general purpose wrapper around the AWS PHP SDK, but WP Offload Media most definitely is not.

@jeysaravana
Copy link
Author

@ianmjones ,
Looks similar issue but I can do other things without breaking anything with global object. Now if I have to use my own setting to use Amazon Web Services and default AWS PHP SDK, I would use default SDK library files.
Only missing thing I found is missing this uploadDirectory option.

@ianmjones
Copy link
Member

Closing as a duplicate of #7 as uploading a directory of media that isn't in the Media Library amount to the same thing as handling files outside of the Media Library.

We'll keep this use-case in mind though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants