使用 PHP 上传到 Amazon S3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16256371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using PHP to upload to Amazon S3
提问by Sean
I've spent the last few hours following tutorials for implementing file uploads to Amazon S3 using php. I uploaded the most recent version of Donovan Sch?nknecht's S3 class to my server (as S3.php) and I am trying to use the following code to test upload capability. I know this code will work because I've seen numerous examples in action.
在过去的几个小时里,我一直在学习使用 php 将文件上传到 Amazon S3 的教程。我将最新版本的 Donovan Sch?nknecht 的 S3 类上传到我的服务器(如 S3.php),我正在尝试使用以下代码来测试上传功能。我知道这段代码会起作用,因为我已经看到了许多正在运行的示例。
<?php
require('S3.php');
$s3 = new S3('KEY', 'SECRET KEY');
//insert into s3
$new_name = time() . '.txt';
S3::putObject(
'upload-me.txt',
'bucketName',
$new_name,
S3::ACL_PUBLIC_READ,
array(),
array(),
S3::STORAGE_CLASS_RRS
);
?>
I get an error 500 server error when I attempt to load this page. Additionally, every other reputable tutorial of this nature has given me the same error 500.
当我尝试加载此页面时,出现错误 500 服务器错误。此外,这种性质的其他所有知名教程都给了我同样的错误 500。
I verified that my key and secret key are valid by connecting to S3 with Cyberduck.
我通过使用 Cyberduck 连接到 S3 来验证我的密钥和秘密密钥是否有效。
Does anyone have a clue as to what I could be doing incorrectly?
有没有人知道我可能做错了什么?
Thanks,
谢谢,
Sean
肖恩
回答by Sean
As it turns out, I was missing the cURL extension for PHP and this was causing an issue as the S3 class I was using required the use of cURL. All is working now.
事实证明,我缺少 PHP 的 cURL 扩展,这导致了一个问题,因为我使用的 S3 类需要使用 cURL。现在一切正常。
回答by Jeremy Lindblom
You should also consider using the official AWS SDK for PHP. Examples for using S3 with the SDK can be found in their S3 user guide.
您还应该考虑使用适用于 PHP的官方AWS 开发工具包。可以在他们的S3 用户指南 中找到将 S3 与 SDK 结合使用的示例。
回答by Zedd Index
You can download most recent version of Amazon PHP SDK by running following composer command
您可以通过运行以下 composer 命令下载最新版本的 Amazon PHP SDK
composer require aws/aws-sdk-php
Further configuration to upload file on Amazon s3 are following
在 Amazon s3 上上传文件的进一步配置如下
// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
// Set Amazon s3 credentials
$client = S3Client::factory(
array(
'key' => "your-key",
'secret' => "your secret key"
)
);
try {
$client->putObject(array(
'Bucket'=>'your-bucket-name',
'Key' => 'your-filepath-in-bucket',
'SourceFile' => 'source-filename-with-path',
'StorageClass' => 'REDUCED_REDUNDANCY'
));
} catch (S3Exception $e) {
// Catch an S3 specific exception.
echo $e->getMessage();
}
Get step by step details from here Amazon S3 File Upload Using PHP
从此处获取使用 PHP 的 Amazon S3 文件上传的分步详细信息
回答by Arvind Bhardwaj
Following example worked for me:
以下示例对我有用:
<?php
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$client = S3Client::factory([
'version' => 'latest',
'region' => 'us-west-1',
'credentials' => [
'key' => "<scret-key>",
'secret' => "<my-secret>"
]
]);
try {
$client->putObject([
'Bucket' =>'<my-bucket-name>',
'Key' => '<file-name>',
'SourceFile' => '<file-path-on-server>', // like /var/www/vhosts/mysite/file.csv
'ACL' => 'public-read',
]);
} catch (S3Exception $e) {
// Catch an S3 specific exception.
echo $e->getMessage();
}
Getting security credentials:
获取安全凭证:
- https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/
- https://console.aws.amazon.com/iam/home?#/security_credential
- https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/
- https://console.aws.amazon.com/iam/home?#/security_credential
Getting region code
获取地区代码
回答by RainWalker
I never found a updated Script with Amazons latest sdk. i have made it by myself. it woks as a php commandline interpreter script. give it a try :
我从来没有用亚马逊最新的 sdk 找到更新的脚本。我自己做的。它可以作为一个 php 命令行解释器脚本。试一试 :
回答by Fiaz Ahmad
Here is sample code to upload images on Amazon S3.
这是在 Amazon S3 上上传图像的示例代码。
// Bucket Name
$bucket="BucketName";
if (!class_exists('S3'))require_once('S3.php');
//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'ACCESS_KEY');
if (!defined('awsSecretKey')) define('awsSecretKey', 'ACCESS_Secret_KEY');
$s3 = new S3(awsAccessKey, awsSecretKey);
$s3->putBucket($bucket, S3::ACL_PUBLIC_READ);
if($s3->putObjectFile($tmp, $bucket , $image_name_actual,S3::ACL_PUBLIC_READ) )
{
$message = "S3 Upload Successful.";
$s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
echo "<img src='$s3file'/>";
echo 'S3 File URL:'.$s3file;
}
else{
$message = "S3 Upload Fail.";
}
}
回答by Ziumin
I'm not familiar with S3 API, but i used it as the storage with https://github.com/KnpLabs/Gaufrette. Gaufrette is a library that provides pretty nice abstraction layer above S3 and other file services/systems.
我不熟悉 S3 API,但我将其用作https://github.com/KnpLabs/Gaufrette的存储。Gaufrette 是一个库,它在 S3 和其他文件服务/系统之上提供了非常好的抽象层。
回答by Youssef Subehi
Use this one to Upload Images using a form and it's working Fine for me you may try using it with your code
使用这个使用表单上传图像,它对我来说很好用,您可以尝试将它与您的代码一起使用
$name = $_FILES['photo']['name'];
$size = $_FILES['photo']['size'];
$tmp = $_FILES['photo']['tmp_name'];
//////Upload Process
// Bucket Name
$bucket = 'bucket-name';
require_once('S3.php');
//AWS access info
$awsAccessKey = 'awsAccessKey';
$awsSecretKey = 'awsSecretKey';
//instantiate the class
$s3 = new S3($awsAccessKey, $awsSecretKey);
$s3->putBucket($bucket, S3::ACL_PUBLIC_READ);
//Rename image name.
$actual_image_name = time();
//Upload to S3
if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) )
{
$image='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
}else{
echo 'error uploading to S3 Amazon';
}