AWS SDK .NET 的问题
Problems with AWS SDK .NET
我正在尝试从我的存储桶中检索图像以发送到我的移动应用程序,我目前有直接访问 AWS 的设备,但是我正在添加一层安全性并让我的应用程序(IOS 和 Android) 现在向我的服务器发出请求,然后服务器将响应 DynamoDB 和 S3 数据。
我正在尝试遵循 AWS 为 .Net 提供的文档和代码示例,它们为 DynamoDB 无缝工作,我 运行 遇到 S3 问题。
我的问题是,如果我不提供凭据,我会收到错误消息:
Failed to retrieve credentials from EC2 Instance Metadata Service
这是预期的,因为我设置了 IAM 角色并且只希望我的应用程序和此服务器(将来只有此服务器)可以访问存储桶。
但是当我提供凭据时,就像我为 DynamoDB 提供凭据一样,我的服务器永远等待并且没有收到来自 AWS 的任何响应。
这是我的 C#:
<%@ WebHandler Language="C#" Class="CheckaraRequestHandler" %>
using System;
using System.Web;
using System.Collections.Generic;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using System.IO;
using System.Threading.Tasks;
public class CheckaraRequestHandler : IHttpHandler
{
private const string bucketName = "MY_BUCKET_NAME";
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USEast1;
public static IAmazonS3 client = new AmazonS3Client("MY_ACCESS_KEY", "MY_SECRET_KEY", RegionEndpoint.USEast1);
public void ProcessRequest(HttpContext context)
{
if (context.Request.HttpMethod.ToString() == "GET")
{
string userID = context.Request.QueryString["User"];
string Action = context.Request.QueryString["Action"];
if (userID == null)
{
context.Response.ContentType = "text/plain";
context.Response.Write("TRY AGAIN!");
return;
}
if (Action == "GetPhoto")
{
ReadObjectDataAsync(userID).Wait();
}
var client = new AmazonDynamoDBClient("MY_ACCESS_KEY", "MY_SECRET_KEY", RegionEndpoint.USEast1);
Console.WriteLine("Getting list of tables");
var table = Table.LoadTable(client, "TABLE_NAME");
var item = table.GetItem(userID);
if (item != null)
{
context.Response.ContentType = "application/json";
context.Response.Write(item.ToJson());
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("0");
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
static async Task ReadObjectDataAsync(string userID)
{
string responseBody = "";
try
{
string formattedKey = userID + "/" + userID + "_PROFILEPHOTO.jpeg";
//string formattedKey = userID + "_PROFILEPHOTO.jpeg";
//formattedKey = formattedKey.Replace(":", "%3A");
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucketName,
Key = formattedKey
};
using (GetObjectResponse response = await client.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.
}
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered ***. Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
}
}
当我调试时,这一行永远等待:
using (GetObjectResponse response = await client.GetObjectAsync(request))
这是在我不提供凭据时引发凭据错误的同一行。我在这里遗漏了什么吗?
如有任何帮助,我们将不胜感激。
我怀疑 AWS .NET SDK 存在一些问题,特别是对 S3 的异步调用。
对 dynamoDB 的异步调用工作完美,但 S3 永远挂起。
解决我的问题的方法是简单地删除异步功能(即使在 AWS 文档中,也应该使用异步调用)
之前:
using (GetObjectResponse response = await client.GetObjectAsync(request))
之后:
using (GetObjectResponse response = myClient.GetObject(request))
希望这对遇到此问题的其他人有所帮助。
我正在尝试从我的存储桶中检索图像以发送到我的移动应用程序,我目前有直接访问 AWS 的设备,但是我正在添加一层安全性并让我的应用程序(IOS 和 Android) 现在向我的服务器发出请求,然后服务器将响应 DynamoDB 和 S3 数据。
我正在尝试遵循 AWS 为 .Net 提供的文档和代码示例,它们为 DynamoDB 无缝工作,我 运行 遇到 S3 问题。
我的问题是,如果我不提供凭据,我会收到错误消息:
Failed to retrieve credentials from EC2 Instance Metadata Service
这是预期的,因为我设置了 IAM 角色并且只希望我的应用程序和此服务器(将来只有此服务器)可以访问存储桶。
但是当我提供凭据时,就像我为 DynamoDB 提供凭据一样,我的服务器永远等待并且没有收到来自 AWS 的任何响应。
这是我的 C#:
<%@ WebHandler Language="C#" Class="CheckaraRequestHandler" %>
using System;
using System.Web;
using System.Collections.Generic;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using System.IO;
using System.Threading.Tasks;
public class CheckaraRequestHandler : IHttpHandler
{
private const string bucketName = "MY_BUCKET_NAME";
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USEast1;
public static IAmazonS3 client = new AmazonS3Client("MY_ACCESS_KEY", "MY_SECRET_KEY", RegionEndpoint.USEast1);
public void ProcessRequest(HttpContext context)
{
if (context.Request.HttpMethod.ToString() == "GET")
{
string userID = context.Request.QueryString["User"];
string Action = context.Request.QueryString["Action"];
if (userID == null)
{
context.Response.ContentType = "text/plain";
context.Response.Write("TRY AGAIN!");
return;
}
if (Action == "GetPhoto")
{
ReadObjectDataAsync(userID).Wait();
}
var client = new AmazonDynamoDBClient("MY_ACCESS_KEY", "MY_SECRET_KEY", RegionEndpoint.USEast1);
Console.WriteLine("Getting list of tables");
var table = Table.LoadTable(client, "TABLE_NAME");
var item = table.GetItem(userID);
if (item != null)
{
context.Response.ContentType = "application/json";
context.Response.Write(item.ToJson());
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("0");
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
static async Task ReadObjectDataAsync(string userID)
{
string responseBody = "";
try
{
string formattedKey = userID + "/" + userID + "_PROFILEPHOTO.jpeg";
//string formattedKey = userID + "_PROFILEPHOTO.jpeg";
//formattedKey = formattedKey.Replace(":", "%3A");
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucketName,
Key = formattedKey
};
using (GetObjectResponse response = await client.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.
}
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered ***. Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
}
}
当我调试时,这一行永远等待:
using (GetObjectResponse response = await client.GetObjectAsync(request))
这是在我不提供凭据时引发凭据错误的同一行。我在这里遗漏了什么吗?
如有任何帮助,我们将不胜感激。
我怀疑 AWS .NET SDK 存在一些问题,特别是对 S3 的异步调用。
对 dynamoDB 的异步调用工作完美,但 S3 永远挂起。
解决我的问题的方法是简单地删除异步功能(即使在 AWS 文档中,也应该使用异步调用)
之前:
using (GetObjectResponse response = await client.GetObjectAsync(request))
之后:
using (GetObjectResponse response = myClient.GetObject(request))
希望这对遇到此问题的其他人有所帮助。