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

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

如何在 PHP 中直接将文件从 S3 存储桶复制到 S3 存储桶(适用于 PHP 的 AWS SDK)? 我正在寻找 Ruby 的示例,即: 但是我找不到 php 的单个示例。此外,我在 AWS SDK 中找不到 copy_to() 或 copy_from() 的方法 PHP - Ruby 示例中使用的命令?

请参阅 AWS Github 代码示例。

如果您想了解如何使用 AWS 开发工具包以受支持的编程语言执行任务,那么您应该将此放在第一位。

以下是 Amazon S3 的 PHP 代码示例:

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

这是从 aws-doc-sdk-examples/s3-copying-objects.php at main · awsdocs/aws-doc-sdk-examples · GitHub 复制对象的示例:

<?php
/**
 * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * This file is licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License. A copy of
 * the License is located at
 *
 * http://aws.amazon.com/apache2.0/
 *
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * ABOUT THIS PHP SAMPLE: This sample is part of the SDK for PHP Developer Guide topic at
 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-examples-creating-buckets.html
 *
 */
 
require 'vendor/autoload.php';

use Aws\S3\S3Client;

$sourceBucket = '*** Your Source Bucket Name ***';
$sourceKeyname = '*** Your Source Object Key ***';
$targetBucket = '*** Your Target Bucket Name ***';

$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1'
]);

// Copy an object.
$s3->copyObject([
    'Bucket'     => $targetBucket,
    'Key'        => "{$sourceKeyname}-copy",
    'CopySource' => "{$sourceBucket}/{$sourceKeyname}",
]);

// Perform a batch of CopyObject operations.
$batch = array();
for ($i = 1; $i <= 3; $i++) {
    $batch[] = $s3->getCommand('CopyObject', [
        'Bucket'     => $targetBucket,
        'Key'        => "{targetKeyname}-{$i}",
        'CopySource' => "{$sourceBucket}/{$sourceKeyname}",
    ]);
}
try {
    $results = CommandPool::batch($s3, $batch);
    foreach($results as $result) {
        if ($result instanceof ResultInterface) {
            // Result handling here
        }
        if ($result instanceof AwsException) {
            // AwsException handling here
        }
    }
} catch (\Exception $e) {
    // General error handling here
}

也可以在创建 S3Client 对象期间传递凭证参数,如下所示:

$s3 = new S3Client([
  'version' => 'latest',
  'region'  => 'us-east-1',
  'credentials' => [
    'key'    => '**client key**',
    'secret' => '**client secret**',
  ],
]);

这是一个执行文件复制、存储桶到存储桶的函数:

  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'参数数组如果使用其他方式可以跳过。还要设置您需要的区域参数。不支持不同区域之间的复制。很简单,就是参数没那么直观