Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

使用 PHP 开发工具包低级别 API 以多个段的形式上传文件

本主题将指导您完成使用适用于 PHP 的 AWS 开发工具包中的低级别分段上传类以多个分段的形式上传文件的过程。

注意

本主题假定您已按照 使用适用于 PHP 的 AWS 开发工具包和运行 PHP 示例 的说明执行操作,并正确安装了 适用于 PHP 的 AWS 开发工具包。

PHP 开发工具包低级别 API 分段文件上传过程

1

使用 Aws\S3\S3Clientfactory() 方法创建 Amazon S3 客户端的实例。

2

通过执行 Aws\S3\S3Client::createMultipartUpload() 方法开始分段上传。必须在 array 参数的必需键 (BucketKey) 中提供存储桶名称和键名称。

从响应正文检索并保存 UploadIDUploadID 会在随后的每个分段上传操作中使用。

3

通过对每个文件段执行 Aws\S3\S3Client::uploadPart() 方法来分段上传文件,直至达到文件末尾。upload_part() 的必需 array 参数键是 BucketKeyUploadIdPartNumber。对于用于上传每个连续文件段的每个后续 PartNumber 调用,必须递增作为 upload_part() 键的参数传递的值。

将每个 upload_part() 方法调用的响应保存在数组中。每个响应都包括以后完成分段上传时您将需要的 ETag 值。

4

执行 Aws\S3\S3Client::completeMultipartUpload() 方法以完成分段上传。completeMultipartUpload() 的必需 array 参数是 BucketKeyUploadId

以下 PHP 代码示例演示如何使用 PHP 开发工具包低级别 API 以多个段的形式上传文件。

Copy
use Aws\S3\S3Client; $bucket = '*** Your Bucket Name ***'; $keyname = '*** Your Object Key ***'; $filename = '*** Path to and Name of the File to Upload ***'; // 1. Instantiate the client. $s3 = S3Client::factory(); // 2. Create a new multipart upload and get the upload ID. $response = $s3->createMultipartUpload(array( 'Bucket' => $bucket, 'Key' => $keyname )); $uploadId = $response['UploadId']; // 3. Upload the file in parts. $file = fopen($filename, 'r'); $parts = array(); $partNumber = 1; while (!feof($file)) { $result = $s3->uploadPart(array( 'Bucket' => $bucket, 'Key' => $key, 'UploadId' => $uploadId, 'PartNumber' => $partNumber, 'Body' => fread($file, 5 * 1024 * 1024), )); $parts[] = array( 'PartNumber' => $partNumber++, 'ETag' => $result['ETag'], ); } // 4. Complete multipart upload. $result = $s3->completeMultipartUpload(array( 'Bucket' => $bucket, 'Key' => $key, 'UploadId' => $uploadId, 'Parts' => $parts, )); $url = $result['Location']; fclose($file);

例 使用低级别分段上传 PHP 开发工具包 API 将文件上传到 Amazon S3 存储桶

以下 PHP 代码示例使用低级别 PHP API 分段上传将文件上传到 Amazon S3 存储桶。有关运行本指南中的 PHP 示例的信息,请参阅运行 PHP 示例

Copy
<?php // Include the AWS SDK using the Composer autoloader require 'vendor/autoload.php'; use Aws\S3\S3Client; $bucket = '*** Your Bucket Name ***'; $keyname = '*** Your Object Key ***'; $filename = '*** Path to and Name of the File to Upload ***'; // 1. Instantiate the client. $s3 = S3Client::factory(); // 2. Create a new multipart upload and get the upload ID. $result = $s3->createMultipartUpload(array( 'Bucket' => $bucket, 'Key' => $keyname, 'StorageClass' => 'REDUCED_REDUNDANCY', 'ACL' => 'public-read', 'Metadata' => array( 'param1' => 'value 1', 'param2' => 'value 2', 'param3' => 'value 3' ) )); $uploadId = $result['UploadId']; // 3. Upload the file in parts. try { $file = fopen($filename, 'r'); $parts = array(); $partNumber = 1; while (!feof($file)) { $result = $s3->uploadPart(array( 'Bucket' => $bucket, 'Key' => $keyname, 'UploadId' => $uploadId, 'PartNumber' => $partNumber, 'Body' => fread($file, 5 * 1024 * 1024), )); $parts[] = array( 'PartNumber' => $partNumber++, 'ETag' => $result['ETag'], ); echo "Uploading part {$partNumber} of {$filename}.\n"; } fclose($file); } catch (S3Exception $e) { $result = $s3->abortMultipartUpload(array( 'Bucket' => $bucket, 'Key' => $keyname, 'UploadId' => $uploadId )); echo "Upload of {$filename} failed.\n"; } // 4. Complete multipart upload. $result = $s3->completeMultipartUpload(array( 'Bucket' => $bucket, 'Key' => $keyname, 'UploadId' => $uploadId, 'Parts' => $parts, )); $url = $result['Location']; echo "Uploaded {$filename} to {$url}.\n";

相关资源

本页内容: