简体   繁体   中英

AWS PHP SDK using a FORM to upload to an S3 Bucket

I have the following form

<form id="uploadFile" enctype="multipart/form-data" method="post" apaction="uptos3.php">
<input type="file" class="form-control" name="file" id="file"></input>
<br>
<button type="submit" name="uploadtoS3">Upload to S3</button>

</form>

And my code, on the same page as the form, to load an S3 Object to a S3 Bucket.

try {
    // Upload data.
    $result = $s3Client->putObject([
        'Bucket' => 'mybucketS3/folderS3/',
        'Key'    => $_FILES['file']['name'],
        'Body'   => $_FILES['file']['tmp_name'],
        'ACL'    => 'public-read'
    ]);

    // Print the URL to the object.
    echo $result['ObjectURL'] . PHP_EOL;
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}


How could I make, that when a user submits the file, that file is directly stored onto S3.

Thanks.

You need to post the file to PHP code that then uses S3 PHP API to place the file into a bucket. Here is the PHP code example that shows you how to upload an object into a bucket.

https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/php/example_code/s3/PutObject.php

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