c# - 从 amazon s3 下载文件时出错

标签 c# .net amazon-web-services amazon-s3

我正在尝试从 amazon s3 下载文件。为此,我正在使用 .NET sdk。我写下了以下代码:

GetObjectRequest request = new GetObjectRequest()
    .WithKey("abc/xyz.jpg")
    .WithBucketName(appConfig);

using (GetObjectResponse response = s3Client.GetObject(request))
{
    int numBytesToRead = (int)response.ContentLength;
    if (numBytesToRead > 0)
    {
        int numBytesRead = 0;
        byte[] buffer = new byte32768;
        using (FileStream fstream = new FileStream(@"C:\abc\xyz.jpg", FileMode.Create))
        {
            using (Stream responseStream = response.ResponseStream)
            {
                do
                {
                    numBytesRead = responseStream.Read(buffer, 0, buffer.Length);
                    fstream.Write(buffer, 0, numBytesRead);
                }
                while (numBytesRead > 0);
            }
        }
    }
}

使用此代码我可以下载文件。但是现在我想下载多个文件。首先,我列出了 s3 上可用的所有文件,然后将其存储在数组列表中,然后为每个文件调用我的下载函数。

在使用 (GetObjectResponse response = s3Client.GetObject(request)) 调用下载函数时运行列表文件方法后出现异常。

{“拒绝访问”}

Stack trace:
at Amazon.S3.AmazonS3Client.processRequestError(String actionName, HttpWebRequest request, WebException we, HttpWebResponse errorResponse, String requestAddr, WebHeaderCollection& respHdrs, Type t, Exception& cause)
at Amazon.S3.AmazonS3Client.handleHttpWebErrorResponse(S3Request userRequest, WebException we, HttpWebRequest request, HttpWebResponse httpResponse, Exception& cause, HttpStatusCode& statusCode)
at Amazon.S3.AmazonS3Client.getResponseCallback[T](IAsyncResult result)
at Amazon.S3.AmazonS3Client.endOperation[T](IAsyncResult result)
at Amazon.S3.AmazonS3Client.EndGetObject(IAsyncResult asyncResult)
at Amazon.S3.AmazonS3Client.GetObject(GetObjectRequest request)
at ScreenshotsUploader.ScreenshotsUploader.downloadFile(AmazonS3 s3Client, NameValueCollection appConfig) in c:\Users\xyz\Documents\Visual Studio 2012\Projects\ScreenshotsUploader\ScreenshotsUploader\ScreenshotsUploader.cs:line 220
at ScreenshotsUploader.ScreenshotsUploader.GetServiceOutput() in c:\Users\xyz\Documents\Visual Studio 2012\Projects\ScreenshotsUploader\ScreenshotsUploader\ScreenshotsUploader.cs:line 55

状态码:

System.Net.HttpStatusCode.Forbidden

我需要做什么来解决这个问题?

我也尝试过不同的下载文件的方法,但得到了同样的错误:

第二种方法:

string fileToDownload = @"xyzjpg";
string saveToo = @"C:\xyz.jpg";
TransferUtilityDownloadRequest request = new TransferUtilityDownloadRequest().WithBucketName(appConfig).WithKey(fileToDownload).WithFilePath(saveToo);
TransferUtility tu = new TransferUtility(s3Client);
tu.Download(request);

提前致谢。

最佳答案

public Stream DownloadS3Object(string awsBucketName, string keyName)
{

    using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client())
    {
        Stream imageStream = new MemoryStream();
        GetObjectRequest request = new GetObjectRequest { BucketName = awsBucketName, Key = keyName };
        using (GetObjectResponse response = client.GetObject(request))
        {
            response.ResponseStream.CopyTo(imageStream);
        }
        imageStream.Position = 0;
        // Clean up temporary file.
        // System.IO.File.Delete(dest);
        return imageStream;
    }
}

在函数中传递值获取流路径使用下面的方法将其保存在文件夹中。

SaveStreamToFile(foldername + "/", MainStreamAwsPath);

然后您可以应用简单的 C# 代码来下载该文件夹。

关于c# - 从 amazon s3 下载文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14532865/

相关文章:

amazon-web-services - 查看 AWS CloudWatch 中的实例数量/正确配置实例扩展

javascript - 是否可以撤销 AWS Cognito IdToken?

c# - 如何放置使用Glass View的静态 View 渲染?

c# - 数字表 Oracle 存储过程参数,如何从 C# 传递它

c# - 在 Silverlight 中列出附加属性

.net - 无法在 Windows XP 上运行 Visual Studio 2012 程序

c# - .net core 1.0 visual studio 引用外部 dll

c# - 应用程序在 Release模式下崩溃,而不是在 Debug模式下崩溃 - Xamarin Forms

c# - 查找匹配字符串算法

amazon-web-services - 节点 Lambda AWS 超时错误 : Socket timed out without establishing a connection to cloudformation