Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何直接从url上传文件到S3 bucket_Php_Amazon S3_Cakephp_Twilio - Fatal编程技术网

Php 如何直接从url上传文件到S3 bucket

Php 如何直接从url上传文件到S3 bucket,php,amazon-s3,cakephp,twilio,Php,Amazon S3,Cakephp,Twilio,我从我的用户那里收到一些彩信。那些彩信是通过twilio发送的。所以twilio将这些文件存储到他们的服务器中,我可以从twilio访问这些文件。但在我的例子中,我需要将这些文件存储到S3中,并从S3显示到我们的系统中。我可以将这些文件存储到本地文件夹或服务器中。但我没有找到任何方法直接从url将文件存储到S3中。 这就是我从url存储到本地目录的步骤 // url of my file. Mostly it will be image. $url = 'urlofmyfile'; // Pat

我从我的用户那里收到一些彩信。那些彩信是通过twilio发送的。所以twilio将这些文件存储到他们的服务器中,我可以从twilio访问这些文件。但在我的例子中,我需要将这些文件存储到S3中,并从S3显示到我们的系统中。我可以将这些文件存储到本地文件夹或服务器中。但我没有找到任何方法直接从url将文件存储到S3中。 这就是我从url存储到本地目录的步骤

// url of my file. Mostly it will be image.
$url = 'urlofmyfile';
// Path where I am saving. Keeping for jpg for now
$img = 'file/sms/file.jpg';
// saving the file into the folder
file_put_contents($img, file_get_contents($url));
如果有人想直接上传到我的系统中,我就是这样把文件保存到S3中的。例如,如果任何用户想要上传他们的个人资料图片

public function saveToS3Bucket($uploadFileName, $imageTmpName) {
    $s3Client = new \Aws\S3\S3Client([
        'version' => env('S3_BUCKET_VERSION'),
        'region'  => env('S3_BUCKET_REGION'),
        'credentials' => array(
            'key'    => env('S3_BUCKET_KEY'),
            'secret' => env('S3_BUCKET_SECRET'),
        )
    ]);
    try {
        $s3Client->putObject([
            'Bucket' => env('S3_BUCKET_NAME'),
            'Key'    => $uploadFileName,
            'SourceFile' => $imageTmpName,
            'StorageClass' => 'REDUCED_REDUNDANCY',
            'ACL' => 'public-read'
        ]);
        return true;
    } catch (S3Exception $e) {
        echo $e->getMessage() . PHP_EOL;
        return false;
    }
}
上述代码工作正常。但我没有找到任何方法从url存储到S3中。请注意,我正在用CakePHP编写代码。

看看下面的内容,它应该会为您指明正确的方向

它来源于这个Twilio博客:

const axios = require('axios');
let AWS = require('aws-sdk');
const S3UploadStream = require('s3-upload-stream');

exports.handler = async function(context, event, callback) {

// Set the region
AWS.config.update({region: 'us-west-2'});
AWS.config.update({ accessKeyId: context.AWSaccessKeyId, secretAccessKey: context.AWSsecretAccessKey });

// The name of the bucket that you have created
const 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 bucket
let 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)
    })
  }
};