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.

上传文件

以下任务将引导您使用高级别的 Java 类来上传文件。API 提供了 方法的多个变体 (称为重载upload),使您可以轻松地上传数据。

高级别 API 文件上传过程

1

创建 TransferManager 类的实例。

2

根据您是从文件还是从流上传数据,执行任意一个 TransferManager.upload 重载。

以下 Java 代码示例演示了上述任务。

以下 Java 代码示例将文件上传到 Amazon S3 存储桶。有关如何创建和测试有效示例的说明,请参阅 测试 Java 代码示例

Copy
import java.io.File; import com.amazonaws.AmazonClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.transfer.TransferManager; import com.amazonaws.services.s3.transfer.Upload; public class UploadObjectMultipartUploadUsingHighLevelAPI { public static void main(String[] args) throws Exception { String existingBucketName = "*** Provide existing bucket name ***"; String keyName = "*** Provide object key ***"; String filePath = "*** Path to and name of the file to upload ***"; TransferManager tm = new TransferManager(new ProfileCredentialsProvider()); System.out.println("Hello"); // TransferManager processes all transfers asynchronously, // so this call will return immediately. Upload upload = tm.upload( existingBucketName, keyName, new File(filePath)); System.out.println("Hello2"); try { // Or you can block and wait for the upload to finish upload.waitForCompletion(); System.out.println("Upload complete."); } catch (AmazonClientException amazonClientException) { System.out.println("Unable to upload file, upload was aborted."); amazonClientException.printStackTrace(); } } }