简体   繁体   English

如何将文件从 S3 存储桶复制到 PHP(AWS SDK for PHP)中的 S3 存储桶?

[英]How to copy files from S3 bucket to S3 bucket in PHP (AWS SDK for PHP)?

How to copy files from S3 bucket to S3 bucket directly in PHP (AWS SDK for PHP)?如何直接在 PHP(AWS SDK for PHP)中将文件从 S3 存储桶复制到 S3 存储桶? I'm finding examples for Ruby, ie: https://stackoverflow.com/a/10418427/2899889 But I can not find a single example for php.我正在寻找 Ruby 的示例,即: https://stackoverflow.com/a/10418427/2899889但我找不到 ZE1BFD762321E409CEEZE8196B 的单个示例Also I could not find methods copy_to() or copy_from() in AWS SDK for PHP - the commands used in Ruby examples?此外,我在 AWS SDK 中找不到用于 PHP 的方法 copy_to() 或 copy_from() - Ruby 示例中使用的命令?

Refer to the AWS Github for Code Examples.有关代码示例,请参阅AWS Github

This is the 1st place you should when you want to learn how to perform tasks using the AWS SDK in a supported programming language.当您想了解如何以受支持的编程语言使用 AWS SDK 执行任务时,这是您应该的第一个地方。

Here are the PHP code examples for Amazon S3:以下是 Amazon S3 的 PHP 代码示例:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code/s3 https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code/s3

Here is an function that does file copying, bucket to bucket:这是一个 function 执行文件复制,桶到桶:

  function s3toS3FileCopy($sourceBucket, $sourceFilePath, $targetBucket, $targetFilePath) {
    $env = getenv();
    $s3 = new S3Client([
      'version' => 'latest',
      'region'  => 'eu-central-1',
      'credentials' => [
        'key'    => $env['S3_ACCESS_KEY'],
        'secret' => $env['S3_SECRET_KEY'],
      ],
    ]);

    $s3->copyObject([
      'Bucket' => $targetBucket,
      'Key' => $targetFilePath,
      'CopySource' => "{$sourceBucket}/{$sourceFilePath}",
    ]);
  }

'credentials' parameter array can be skipped if some other way is used.如果使用其他方式,可以跳过“凭据”参数数组。 Also set the region parameter you need.还要设置您需要的区域参数。 Copying between different regions is not supported.不支持不同区域之间的复制。 It's simple, but parameters are no so intuitive.这很简单,但参数并不那么直观。

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

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