简体   繁体   English

使用 PHP 将 object 从一个 Amazon S3 存储桶复制到另一个存储桶

[英]Copying an object from one Amazon S3 bucket to another using PHP

I'm trying to copy a folder from one S3 bucket to another using PHP but it is causing me problems.我正在尝试使用 PHP 将一个文件夹从一个 S3 存储桶复制到另一个存储桶,但这给我带来了问题。 I can create the folder I want but I cannot copy from one folder to another.我可以创建我想要的文件夹,但我无法从一个文件夹复制到另一个文件夹。 Here is my code.这是我的代码。 Any help would be greatly appreciated任何帮助将不胜感激

$key = '123';
$secret = '123';

$bucket = 'bck-users';
$keyname = $username;

try {
            //Create bucket for user
            $s3 = Aws\S3\S3Client::factory(array(
            'region' => 'eu-west-1',
            'version' => '2006-03-01',
            'credentials' => array(
              'key' => $key,
              'secret' => $secret,
                )
            ));

            $result = $s3->putObject(array(
              'Bucket' => $bucket,
              'Key'    => $keyname,
            ));

            echo $result['ObjectURL'] . "\n";

        }
        catch (Exception $e){
          echo $e->getMessage() . "\n";
        }

        //Copy default files to bucket
        $sourceBucket = 'bck-users';
        $sourceKeyname = 'default';
        $targetBucket = $username;
        $targetKeyname = 'default';             

        // Instantiate the client.
        $s3 = S3Client::factory(array(
            'region' => 'eu-west-1',
            'version' => '2006-03-01',
            'credentials' => array(
              'key' => $key,
              'secret' => $secret,);

        // Perform a batch of CopyObject operations.
        $batch = array();
        $batch[] = $s3->getCommand('CopyObject', array(
                'Bucket'     => $targetBucket,
                'Key'        => $targetKeyname,
                'CopySource' => $sourceBucket/$sourceKeyname,
            ));
        }
        try {
            $successful = $s3->execute($batch);
        } 
        catch (Exception $e){
          echo $e->getMessage() . "\n";
        }
$s3Client = new S3Client([
  'region' => 'ap-southeast-1',
  'version' => 'latest',
  'credentials' => [
    'key' => Yii::$app->params['key'],
    'secret' => Yii::$app->params['secret'],
  ]
]);

$result = $s3Client->copyObject([
  'Bucket' => Yii::$app->params['bucket'],
  'CopySource' => 'sourcebucketname/objectkey',
  'Key' => 'tragetbucketname/tragetkey',
]);

return $result;

Hi to everyone who comes here.大家好来到这里。

I suggest a copy method that can copy to another bucket.我建议一种可以复制到另一个桶的copy方法。

public function CopyFile(string $key, $to)
{
    $s3Client = new S3Client([
                'version' => 'latest',
                'region' => self::AWS_REGION,
                'credentials' => [
                    'key' => self::AWS_KEY,
                    'secret' => self::AWS_SECRET
                ]
            ]);
    $s3Client->copy(self::BUCKET_NAME, $key, self::BUCKET_NAME, $to);
}

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

相关问题 使用php将文件从一个文件夹复制到Amazon S3存储桶中的另一个文件夹 - Copying files from one folder to another in Amazon S3 bucket using php 自动将文件从一个存储桶移动到另一个存储桶并在Amazon S3中公开 - Automatically move file from one bucket to another bucket and make public in Amazon S3 将一个目录复制到同一Amazon S3存储桶中的另一个目录 - Copy one directory to another in same amazon s3 bucket 用于php的亚马逊s3 SDK无法获取Bucket对象列表 - Amazon s3 SDK for php can not get Bucket object list 从Amazon S3存储桶对象获取图像URL - Getting image url from amazon s3 bucket object 使用PHP将整个文件夹上传到Amazon S3存储桶 - Upload entire folder to Amazon S3 bucket using PHP PHP - Zend Amazon AWS S3 - 从桶中加载图像 - PHP - Zend Amazon AWS S3 - Loading image from bucket 如何使用php中的访问和秘密密钥从Amazon S3存储桶下载图像? - how to download image from amazon S3 bucket using access and secret key in php? 删除 Amazon S3 中的对象或存储桶? - Delete object or bucket in Amazon S3? 如何在PHP中将所有文件(不使用副本)和文件夹从一个文件夹移动到S3存储桶的另一个文件夹,不能使用复制功能 - How to move all files (without using copy) and folder from one folder to another of S3 bucket in PHP ,Cannot use copy function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM