我正在尝试从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存储的文件,并将其存储在ArrayList中,然后对每个文件调用我的下载函数。
运行list file方法后,当我在使用(GetObjectResponse response = s3Client.GetObject(request))后调用下载函数时,我会收到异常。
{"Access Denied"}
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);
提前致谢。