4

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!

4 Answers 4

12

Try this way

$s3->create_object($bucket, 'folder/filename', $options)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes the keep in mind that there are no folders in S3. You can use folder-like names such as folder/filename to present file hierarchies, but these are all simply files in a common data storage bucket. So you need to explicitly prepend your file names with any folder-like identifier you would like to use.
7

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;
}

Comments

1

Since all the answers here are pretty old, and this is the first result on Google, I'm posting the updated version:

$client = new \Aws\S3\S3Client([
    'region'  => '-- your region --',
    'version' => 'latest',
    'credentials' => [
        'key'    => "-- your IAM --",
        'secret' => "-- your secret --",
    ]
]);

$dir = '/local/directory';
$bucket = 'my-bucket';
$keyPrefix = '';
$options =[
    'params'      => ['ACL' => 'public-read'],
    'concurrency' => 20,
    'debug'       => true
];

$client->uploadDirectory($dir, $bucket, $keyPrefix, $options);

Comments

-1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.