简体   繁体   中英

PHP AWS S3 sdk: How to generate pre signed url to upload a file to a folder in the S3 bucket using PHP?

I am trying to create a pre signed url to upload a file to a folder in the S3 bucket using PHP. I am able to generate url to upload in bucket, but couldn't figure out where to mention folder name. Below is my code.

$object = 'test_103.jpg';
$bucket = $config['s3_input']['bucket'];
$expiry = new DateTime('+10 minutes');

$command = $s3_input->getCommand(
      'PutObject',
      [
          'Bucket' => $bucket,
          'Key' => $object
      ]
  );
$signedRequest = $s3_input->createPresignedRequest($command,'+10 minutes');
$signedUploadUrl = $signedRequest->getUri();

echo $signedUploadUrl;

In above code how do I pass folder name in which I want to create pre signed url?

S3 does not have the hierarchy/directory structure, so keep that in mind, It becomes important when later you want to "move" or "rename" a "folder" on S3

https://serverfault.com/questions/435827/what-is-the-difference-between-buckets-and-folders-in-amazon-s3

Direct answer for your question : Add the slashes "/" in your "Key" to achieve the effect you want

Also, from the document

$commands[] = $s3Client->getCommand('PutObject', array(
    'Bucket' => 'SOME_BUCKET',
    'Key'    => 'photos/photo01.jpg',
    'Body'   => fopen('/tmp/photo01.jpg', 'r'),
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM