使用php将文件上传到AWS S3

时间:2021-08-12 10:47:27

I am trying to create a php script which will be able to upload a text file to my ASW S3 bucket.

我正在尝试创建一个php脚本,它可以将文本文件上传到我的ASW S3存储桶。

I have tried the method which is there on AWS site but sadly that ain't proper, I mean it's not end to end.

我已经尝试过AWS网站上的方法,但遗憾的是这不合适,我的意思是它不是端到端的。

I have installed the AWS PHP SDK on my instance.

我在我的实例上安装了AWS PHP SDK。

Then I did what's written in the sample code i.e.

然后我做了示例代码中写的内容,即

<?php
    use Aws\S3\S3Client;

$bucket = 'cst';
$keyname = 'sampleUpload';
// $filepath should be absolute path to a file on disk                      
$filepath = '/var/www/html/po/si/mag/sahara.txt';

// Instantiate the client.
$s3 = S3Client::factory();

// Upload a file.
$result = $s3->putObject(array(
    'Bucket'       => $bucket,
    'Key'          => $keyname,
    'SourceFile'   => $filepath,
    'ContentType'  => 'text/plain',
    'ACL'          => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY',
    'Metadata'     => array(    
        'param1' => 'value 1',
        'param2' => 'value 2'
    )
));

echo $result['ObjectURL'];

?>

Obviously, I haven't added the aws key nor the aws secret key so it won't work. But then nothing is specified in the tutorial either. So am kinda lost.

显然,我没有添加aws密钥和aws密钥,所以它不起作用。但是,教程中也没有指定任何内容。所以有点失落。

Secondly, I tried using this code :

其次,我尝试使用此代码:

It's also not working.

它也不起作用。

Thirdly, I tried this article.

第三,我试过这篇文章。

It's working when am using it with html but I am not really able to create a php only script where I can just specify the file location, and the file get's uploaded to the server.

当它与html一起使用时它正在工作,但我真的不能创建一个只有php的脚本,我可以只指定文件位置,并将文件get上传到服务器。

Any help is highly appreciated. I searched a lot, but couldn't find anything useful.

任何帮助都非常感谢。我搜索了很多,但找不到任何有用的东西。

1 个解决方案

#1


1  

Just a guess, but did you add your credentials inside your HTML code using hidden inputs? Cause I just had a very quick look at this page: https://aws.amazon.com/articles/1434/ and it seems like you can set your credentials using HTML. And my guess is the class will automatically take care of that.

只是一个猜测,但您是否使用隐藏的输入在HTML代码中添加了凭据?因为我只是快速浏览一下这个页面:https://aws.amazon.com/articles/1434/,好像你可以使用HTML设置你的凭据。我的猜测是班级会自动处理。

If my guess is right, you do need to add the credentials to your instance:

如果我的猜测正确,您需要将凭据添加到您的实例:

// Instantiate the client.
$s3 = S3Client::factory();

like

喜欢

// Instantiate the client.
$s3 = S3Client::factory(array(
  'version' => 'latest',
  'region'  => 'us-west-2', //add correct region
  'credentials' => array(
    'key' => <YOUR_AWS_KEY>,
    'secret'  => <YOUR_AWS_SECRET>
  )
));

It probably depends on the version of the sdk you're using, whether you need above mentioned code or this one (notice the missing credentials array):

它可能取决于您正在使用的sdk的版本,无论您是否需要上述代码或此代码(请注意缺少的凭证数组):

// Instantiate the client.
$s3 = S3Client::factory(array(
  'version' => 'latest',
  'region'  => 'us-west-2', //add correct region
  'key' => <YOUR_AWS_KEY>,
  'secret'  => <YOUR_AWS_SECRET>
));

EDIT: Just to show what exactly worked in my case, this is my complete code. The path I executed: http://myurl.com/index.php?path=./test.txt

编辑:只是为了显示我的案例究竟有用,这是我的完整代码。我执行的路径:http://myurl.com/index.php?path =。/ test.txt

code:

码:

require __DIR__ . '/vendor/autoload.php';

use Aws\S3\S3Client;

$bucket = 'sdl-images';
$keyname = '*** Your Object Key ***';
// $filepath should be absolute path to a file on disk                      
$filepath = $_GET['path'];

// Instantiate the client.
$s3 = S3Client::factory(array(
  'version' => 'latest',
  'region'  => <YOUR_REGION E.G. eu-west-1>,
  'credentials' => array(
    'key' => <YOUR_AWS_KEY>,
    'secret'  => <YOUR_AWS_SECRET>
  )
));

// Upload a file.
$result = $s3->putObject(array(
    'Bucket'       => $bucket,
    'Key'          => $keyname,
    'SourceFile'   => $filepath,
    'ContentType'  => 'text/plain',
    'ACL'          => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY'
));

echo $result['ObjectURL'];

#1


1  

Just a guess, but did you add your credentials inside your HTML code using hidden inputs? Cause I just had a very quick look at this page: https://aws.amazon.com/articles/1434/ and it seems like you can set your credentials using HTML. And my guess is the class will automatically take care of that.

只是一个猜测,但您是否使用隐藏的输入在HTML代码中添加了凭据?因为我只是快速浏览一下这个页面:https://aws.amazon.com/articles/1434/,好像你可以使用HTML设置你的凭据。我的猜测是班级会自动处理。

If my guess is right, you do need to add the credentials to your instance:

如果我的猜测正确,您需要将凭据添加到您的实例:

// Instantiate the client.
$s3 = S3Client::factory();

like

喜欢

// Instantiate the client.
$s3 = S3Client::factory(array(
  'version' => 'latest',
  'region'  => 'us-west-2', //add correct region
  'credentials' => array(
    'key' => <YOUR_AWS_KEY>,
    'secret'  => <YOUR_AWS_SECRET>
  )
));

It probably depends on the version of the sdk you're using, whether you need above mentioned code or this one (notice the missing credentials array):

它可能取决于您正在使用的sdk的版本,无论您是否需要上述代码或此代码(请注意缺少的凭证数组):

// Instantiate the client.
$s3 = S3Client::factory(array(
  'version' => 'latest',
  'region'  => 'us-west-2', //add correct region
  'key' => <YOUR_AWS_KEY>,
  'secret'  => <YOUR_AWS_SECRET>
));

EDIT: Just to show what exactly worked in my case, this is my complete code. The path I executed: http://myurl.com/index.php?path=./test.txt

编辑:只是为了显示我的案例究竟有用,这是我的完整代码。我执行的路径:http://myurl.com/index.php?path =。/ test.txt

code:

码:

require __DIR__ . '/vendor/autoload.php';

use Aws\S3\S3Client;

$bucket = 'sdl-images';
$keyname = '*** Your Object Key ***';
// $filepath should be absolute path to a file on disk                      
$filepath = $_GET['path'];

// Instantiate the client.
$s3 = S3Client::factory(array(
  'version' => 'latest',
  'region'  => <YOUR_REGION E.G. eu-west-1>,
  'credentials' => array(
    'key' => <YOUR_AWS_KEY>,
    'secret'  => <YOUR_AWS_SECRET>
  )
));

// Upload a file.
$result = $s3->putObject(array(
    'Bucket'       => $bucket,
    'Key'          => $keyname,
    'SourceFile'   => $filepath,
    'ContentType'  => 'text/plain',
    'ACL'          => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY'
));

echo $result['ObjectURL'];