简体   繁体   English

使用PHP上传到存储桶中的AWS S3'文件夹'

[英]Upload to AWS S3 'folder' inside bucket with PHP

Apologies, I'm not an AWS user. 抱歉,我不是AWS用户。 I need to upload a file to a 'folder' in an existing bucket within AWS S3. 我需要将文件上传到AWS S3中现有存储桶中的“文件夹”。 I understand that there is no concept of 'folders' within S3, so how I could specify the 'folder' it needs to go to? 我知道S3中没有“文件夹”的概念,那么我如何指定需要去的“文件夹”呢?

My uploaded image needs to follow an existing structure of 我上传的图片需要遵循现有的结构

https://s3.amazonaws.com/my.bucket.name/newfolder/test_image.png

The code I have below works, but it puts the image into the root bucket, whereas I need to put it into a new folder ($newfolder). 我下面的代码可以工作,但是它将图像放入根存储桶,而我需要将其放入新文件夹($ newfolder)。

Does anyone know where in the below code I could specify the $newfolder to achieve this? 有谁知道在下面的代码中我可以指定$ newfolder来实现这一目标吗?

Thank you. 谢谢。

<?php
$newfolder = "newfolder";
$bucket = 'my.bucket.name';
require_once('S3.php');

$awsAccessKey = 'MyAccessKey';
$awsSecretKey = 'MySecretKey';

$s3 = new S3($awsAccessKey, $awsSecretKey);
$s3->putBucket($bucket, S3::ACL_PUBLIC_READ);

$actual_image_name = 'test_image.png';

if($s3->putObjectFile('/var/www/html/test/image.png', $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) )
    {
    $image='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
    }
    else
    {
    echo 'error uploading to S3 Amazon';
    }  
?>
$actual_image_name = 'newfolder/test_image.png';

S3 calls this the key inside the bucket . S3将此称为桶中密钥 The key can contain slashes, which would be treated as folders when viewed using an appropriate client. 密钥可以包含斜杠,使用适当的客户端查看时,斜杠将被视为文件夹。 But the concept of "folders" doesn't matter much when dealing with the S3 API nor URLs, so it's just "keys" with slashes in them. 但是,“文件夹”的概念在处理S3 API或URL时并没有多大关系,因此只是带有斜杠的“键”。

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

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