How to upload files to S3 bucket from url directly

✔ Recommended Answer

Have a look at the Twilio Function below, it should point you in the right direction.

It was derived from this Twilio Blog:

Encrypting and Storing Twilio Flex Recordings Off-site

const axios = require('axios');let AWS = require('aws-sdk');const S3UploadStream = require('s3-upload-stream');exports.handler = async function(context, event, callback) {// Set the regionAWS.config.update({region: 'us-west-2'});AWS.config.update({ accessKeyId: context.AWSaccessKeyId, secretAccessKey: context.AWSsecretAccessKey });// The name of the bucket that you have createdconst BUCKET_NAME = 'winston';const fileUrl = "https://a.b.twil.io/assets/KittehWinston.jpg";const fileName = "winston.jpg";const s3Stream = S3UploadStream(new AWS.S3());// call S3 to retrieve upload file to specified bucketlet upload = s3Stream.upload({Bucket: BUCKET_NAME, Key: fileName, ContentType: 'image/jpeg', ACL: 'public-read'   });const fileUpload = await uploadFile(fileUrl, upload).then(result => callback(null, `success: ${JSON.stringify(result)}`)).catch(err => callback(err.message));async function uploadFile (url, upload) {    const response = await axios({      url,      method: 'GET',      responseType: 'stream'    })      response.data.pipe(upload);      return new Promise((resolve, reject) => {      upload.on('uploaded', resolve)      upload.on('error', reject)    })  }};

Source: stackoverflow.com

Answered By: Alan

Method #2

To upload files to an Amazon S3 bucket from a URL directly, you can use the AWS SDK for PHP. Here's an example of how to do it:

php
use Aws\S3\S3Client; // Initialize an S3 client $client = new S3Client([ 'version' => 'latest', 'region' => 'your_bucket_region', 'credentials' => [ 'key' => 'your_access_key', 'secret' => 'your_secret_key', ], ]); // Set the URL of the file to upload $fileUrl = 'https://example.com/image.jpg'; // Set the name of the file in the S3 bucket $fileName = 'image.jpg'; // Upload the file from the URL to the S3 bucket $result = $client->putObject([ 'Bucket' => 'your_bucket_name', 'Key' => $fileName, 'Body' => fopen($fileUrl, 'r'), 'ACL' => 'public-read', // Set the object ACL ]); // Get the URL of the uploaded file $fileUrl = $result['ObjectURL'];

In this example, we first initialize an S3 client with the appropriate credentials and region. We then set the URL of the file to upload and the desired file name in the S3 bucket. Next, we use the putObject method of the S3 client to upload the file from the URL to the S3 bucket, setting the ACL of the uploaded object to public-read. Finally, we retrieve the URL of the uploaded file.

Note that the putObject method takes an input stream as its Body parameter. In this example, we pass the result of fopen($fileUrl, 'r') to the Body parameter to open a read-only stream to the file at the specified URL. This approach avoids loading the entire file into memory at once, which could cause memory issues for large files.

Comments

Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755