使用.net SDK从Amazon S3存储桶下载文件夹

时间:2018-12-11 05:56:39

标签: c# amazon-web-services amazon-s3

如何使用.net sdk下载s3存储桶中存在的整个文件夹。尝试以下代码,它会抛出无效密钥。我需要下载存储桶中存在的嵌套pesudo文件夹中存在的所有文件,并删除文件下载限制为1000默认。

public static void DownloadFile()
{
var client = new AmazonS3Client(keyId, keySecret, bucketRegion);
        ListObjectsV2Request request = new ListObjectsV2Request
        {
            BucketName = bucketName + "/private/TargetFolder",         
            MaxKeys = 1000
        };
        try
        {
            ListObjectsV2Response bucketResponse = client.ListObjectsV2(request);
            foreach (S3Object o in bucketResponse.S3Objects)
            {
                var getRequest = new GetObjectRequest
                {
                    BucketName = bucketResponse.Name + "/private/TargetFolder",
                    Key = bucketResponse.Name +"/private/TargetFolder/"+ o.Key
                };
                var response = client.GetObject(getRequest);
                response.WriteResponseStreamToFile(downloadLocation + "\\" + o.Key);
                var responseCode = response.HttpStatusCode;
                if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
                {
                    Console.WriteLine($"Success downloaded : {o.Key}");
                }
                else if (response.HttpStatusCode == System.Net.HttpStatusCode.RequestTimeout)
                {
                    Console.WriteLine("Request Timeout error.");
                }
                else if (response.HttpStatusCode == System.Net.HttpStatusCode.ServiceUnavailable)
                {
                    Console.WriteLine("Service Unavailable.");
                }
                else if (response.HttpStatusCode == System.Net.HttpStatusCode.InternalServerError)
                {
                    Console.WriteLine("Internal Server error.");
                }
                else
                {
                    Console.WriteLine("Please check the provided AWS Credentials.");
                }
            }
        }
        catch (AmazonS3Exception amazonS3Exception)
        {
            if (amazonS3Exception.ErrorCode != null &&
                (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
            {
                Console.WriteLine("Please check the provided AWS Credentials.");
            }
            else
            {
                Console.WriteLine(amazonS3Exception.Message);
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.ReadLine();
    }

谢谢!

2 个答案:

答案 0 :(得分:2)

尝试一下,它对我有用

public static void DownloadFile()
        {
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls | SecurityProtocolType.Tls;

            var client = new AmazonS3Client(keyId, keySecret, bucketRegion);
            ListObjectsRequest request = new ListObjectsRequest();

            request.BucketName = "BUCKET_NAME";
            request.Prefix = "/private/TargetFolder";
            request.Delimiter = "/";
            request.MaxKeys = 1000;

            ListObjectsResponse response = client.ListObjects(request);
            var x = response.S3Objects;

            foreach (var objt in x)
            {
                GetObjectRequest request1 = new GetObjectRequest();
                request1.BucketName = "BUCKET_NAME";
                request1.Key = objt.Key;

                GetObjectResponse Response = client.GetObject(request1);

                using (Stream responseStream = Response.ResponseStream)
                {
                    Response.WriteResponseStreamToFile(downloadLocation + "\\" + o.Key);
                }

            }
        }

答案 1 :(得分:0)

如果您在 S3Objects.Count 中总是得到0,请尝试不使用 Delimiter 属性:

public async Task DownloadDirectoryAsync()
{
    var bucketRegion = RegionEndpoint.USEast2;
    var credentials = new BasicAWSCredentials(accessKey, secretKey);
    var client = new AmazonS3Client(credentials, bucketRegion);

    var bucketName = "bucketName";
    var request = new ListObjectsV2Request
    {
        BucketName = bucketName,
        Prefix = "directorey/",
        MaxKeys = 1000
    };

    var response = await client.ListObjectsV2Async(request);
    var utility = new TransferUtility(s3Client);
    var downloadPath = "c:\\your_folder";
    foreach (var obj in response.S3Objects)
    {
        utility.Download($"{downloadPath}\\{obj.Key}", bucketName, obj.Key);
    }
} 

当然,您需要 s3:ListBucket 权限