简体   繁体   中英

AWS S3 PHP can I upload a file directly to a folder in a bucket

What is the syntax for uploading a file directly to a folder in AWS S3 with the php sdk?

I was successful in uploading to a bucket using:

$response = $s3->create_object($bucket, $file_name, array('fileUpload' => $file_path))

tx!

试试这个

$s3->create_object($bucket, 'folder/filename', $options)

Simply do 1 thing, add 'path/' ex: If you have a folder 'videos' in a bucket then you have to add path like this in 'key' =>'videos/video-name-here'.

try {
    // Upload data.
$result = $s3Client->putObject([
    'Bucket' => bucket-name,
    'Key'    => 'videos/video-name', //add path here
    'Body'   => fopen($video, 'r'),
    'ACL'    => 'public-read'
]);
print_r($result['ObjectURL']);

} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

This is another way you can try

    $this->Aws->bucket = 'bucket'
    $old_file = $dir. '/'.old_file_name;       
    $new_file = $dir. '/'.new_file_name;
    $file_permission = 'private';
    $this->Aws->upload($old_file , $new_file , $file_permission);

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