简体   繁体   English

用php直接将文件上传到amazon s3

[英]upload files to amazon s3 directly with php

solved: using this post : http://www.designedbyaturtle.co.uk/2013/direct-upload-to-s3-with-a-little-help-from-jquery/ 解决:使用这篇文章: http//www.designedbyaturtle.co.uk/2013/direct-upload-to-s3-with-a-little-help-from-jquery/

edit: I'm looking for a way to directly upload a big zip file from client side to amazon s3 without first uploading it to my server 编辑:我正在寻找一种方法直接将一个大的zip文件从客户端上传到亚马逊s3,而无需先将其上传到我的服务器

I'm using a small server for my web application with laravel framework I have some users trying to upload big files to my servr (around 300-400m), and my server is to weak and my bandwidth is to low for the user to be able to finish uploading. 我正在使用一个小型服务器用于我的web应用程序与laravel框架我有一些用户试图将大文件上传到我的servr(大约300-400m),我的服务器很弱,我的带宽很低,用户是能够完成上传。

I want the file to be uploaded directly to amazon s3, from browsing around i dont think this can be dont with laravel sdk for amazon 我希望文件直接上传到亚马逊s3,从浏览我不认为这可能不适用于亚马逊laravel sdk

I installed the official sdk and I'm trying to do it like suggested in this post uploading posted file to amazon s3 我安装了官方的sdk,我正在尝试这样做,就像在这篇文章中建议上传发布的文件到亚马逊s3

but not rally sure how to actually send my file to amazon s3, what should i put instad of 'string of your file here': 但不能确定如何实际将我的文件发送到亚马逊s3,我应该把你的文件的字符串放在这里:

 $result = $client->upload(
'BUCKET HERE',
'OBJECT KEY HERE',
'STRING OF YOUR FILE HERE',
'public-read' // public access ACL
);

im getting my file like this : 我得到这样的文件:

$myFile = Input::file('file');

putting $myfile instead of 'string of your file here' doesnt work 把$ myfile而不是'你的文件的字符串'放在这里是行不通的

我用这篇文章解决了这个问题:解决了:使用这篇文章: http//www.designedbyaturtle.co.uk/2013/direct-upload-to-s3-with-a-little-help-from-jquery/

You can upload files directly from the client side to S3, but it's a security risk as you can't validate the file, plus giving anyone access to your S3 buckets is just generally a bad idea. 您可以直接从客户端将文件上传到S3,但由于无法验证文件,因此存在安全风险,而且任何人都可以访问您的S3存储桶通常是一个坏主意。 I would still look at uploading via your Laravel application. 我仍然会看看你的Laravel应用程序上传。

All files uploaded to PHP are stored on your server, most likely in the /tmp directory for Linux and Mac machines. 上传到PHP的所有文件都存储在您的服务器上,很可能存储在Linux和Mac计算机的/tmp目录中。 There's no way to avoid that, the file needs to be stored somewhere. 没有办法避免这种情况,文件需要存储在某个地方。 You obtain an object representing the file using Input::file('file') . 使用Input::file('file')获取表示文件的对象。

The file is an instance of Symfony\\Component\\HttpFoundation\\File\\UploadedFile , which exposes a getRealPath() method. 该文件是Symfony\\Component\\HttpFoundation\\File\\UploadedFile一个实例,它公开了一个getRealPath()方法。 We can then use fopen() to read the contents of the file as a string. 然后我们可以使用fopen()以字符串形式读取文件的内容。 So to upload it to S3, use: 所以要将其上传到S3,请使用:

$client->upload(
  'bucket',
  'key',
  fopen($file->getRealPath(), 'r'),
  'public-read',
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM