我希望下载存储在 S3 中的多个图像。但是现在我只下载一个就足够了。我有对象路径的信息。
当我运行以下代码时,我收到此错误:
遇到错误***。消息:读取对象时“拒绝访问”...
我首先根据我的密钥和访问配置创建一个AmazonS3Client对象以连接到服务器,然后根据我的对象的存储路径发出请求,然后以某种方式读取数据并将其保存在我的设备中。
顺便说一句,在我们的 AWS S3 中,我们有“<em>role”,这意味着要从 Web 平台直接从 S3 下载,我在 AWS 中的用户不足以访问这些对象,我还应该将角色更改为在我登录后可以访问这些图像的人。
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.IO;
using System.Threading.Tasks;
namespace Amazon.DocSamples.S3
{
class GetObjectTest
{
private const string bucketName = "My_Bucket_Name";
private const string keyName = "123/456/789.jpg";
// Specify your bucket region (an example region is shown).
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
private static IAmazonS3 client;
static string accessKey = "***AccessKey***"; // "YourAccessKeyHere";
static string secretKey = "***SecretKey***";
public static void Main()
{
client = new AmazonS3Client(accessKey,secretKey,bucketRegion);
ReadObjectDataAsync().Wait();
}
static async Task ReadObjectDataAsync()
{
string responseBody = "";
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucketName,
Key = keyName
};
using (GetObjectResponse response = awaitclient.GetObjectAsync(request))
using (Stream responseStream = response.ResponseStream)
using (StreamReader reader = new StreamReader(responseStream))
{
string title = response.Metadata["x-amz-meta-title"]; // Assume you have "title" as medata added to the object.
string contentType = response.Headers["Content-Type"];
Console.WriteLine("Object metadata, Title: {0}", title);
Console.WriteLine("Content type: {0}", contentType);
responseBody = reader.ReadToEnd(); // Now you process the response body.
}
}
}
}
我发现以下与访问问题相关的链接: https ://aws.amazon.com/premiumsupport/knowledge-center/s3-troubleshoot-403/