.net - 如何使用适用于 .NET 的 AWS 开发工具包将多个文件异步上传到 Amazon S3?

标签 .net amazon-s3

我正在尝试使用 .NET SDK 在 Amazon S3 上异步上传多个文件。任何让我开始的例子将不胜感激。提前致谢。

最佳答案

Amazon S3AWS SDK for .NET您正在寻找的功能是 Using the High-Level .NET API for Multipart Upload :

The AWS SDK for .NET exposes a high-level API that simplifies multipart upload (see Uploading Objects Using Multipart Upload API). You can upload data from a file, directory, or a stream. [...] You can optionally set advanced options such as the part size you want to use for the multipart upload, number of threads you want to use when uploading the parts concurrently, optional file metadata, the storage class (STANDARD or REDUCED_REDUNDANCY), or ACL. The high-level API provides the TransferUtilityUploadRequest class to set these advanced options. [emphasis mine]

Upload a Directory 中提供了示例片段:

using System;
using System.IO;
using Amazon.S3;
using Amazon.S3.Transfer;

namespace s3.amazon.com.docsamples
{
    class UploadDirectoryMPUHighLevelAPI
    {
        static string existingBucketName = "*** Provide bucket name ***";
        static string directoryPath      = "*** Provide directory name ***";

        static void Main(string[] args)
        {
            try
            {
                TransferUtility directoryTransferUtility =
                    new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));

                // 1. Upload a directory.
                directoryTransferUtility.UploadDirectory(directoryPath,
                                                         existingBucketName);
                Console.WriteLine("Upload statement 1 completed");

                // 2. Upload only the .txt files from a directory. 
                //    Also, search recursively. 
                directoryTransferUtility.UploadDirectory(
                                               directoryPath,
                                               existingBucketName,
                                               "*.txt",
                                               SearchOption.AllDirectories);
                Console.WriteLine("Upload statement 2 completed");

                // 3. Same as 2 and some optional configuration 
                //    Search recursively for .txt files to upload).
                TransferUtilityUploadDirectoryRequest request =
                    new TransferUtilityUploadDirectoryRequest
                    {
                        BucketName = existingBucketName,
                        Directory = directoryPath,
                        SearchOption = SearchOption.AllDirectories,
                        SearchPattern = "*.txt"
                    };

                directoryTransferUtility.UploadDirectory(request);
                Console.WriteLine("Upload statement 3 completed");
            }

            catch (AmazonS3Exception e)
            {
                Console.WriteLine(e.Message, e.InnerException);
            }
        }
    }
}

关于.net - 如何使用适用于 .NET 的 AWS 开发工具包将多个文件异步上传到 Amazon S3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24278799/

相关文章:

.net - 以编程方式指定要加载的 .NET 版本

.net - 在应用程序 web.config 中设置时,IIS 7.5 不压缩 JSON

amazon-web-services - 是什么阻止了我的 S3 存储桶被终止?

go - 在 Golang 中写入/附加到 S3 存储桶

php - KnpGaufette/Symfony2/AmazonS3

c# - 将 ImageMagick 和 GhostScript 与 nuget 结合使用

c# - 使用 HttpRequestException 获取失败请求的响应正文

c# - 正则表达式匹配未完成

java - 模式和匹配器 : How to -- concatenate a pattern and a string for file search

node.js - 使用 NodeJS 传输视频