【问题标题】:Problems with AWS SDK .NETAWS SDK .NET 的问题
【发布时间】:2018-08-03 16:13:28
【问题描述】:

我正在尝试从我的存储桶中检索图像以发送到我的移动应用程序,我目前有设备直接访问 AWS,但是我正在添加一个安全层并让我的应用程序(IOS 和 Android)现在向我的然后服务器将响应 DynamoDB 和 S3 数据。

我正在尝试遵循 AWS 为 .Net 提供的文档和代码示例,它们与 DynamoDB 无缝协作,但我在使用 S3 时遇到了问题。

S3 .NET Documentation

我的问题是,如果我没有提供凭据,我会收到错误消息:

无法从 EC2 实例元数据服务检索凭证

这是意料之中的,因为我设置了 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)) 

这是在我不提供凭据时引发凭据错误的同一行。我在这里有什么遗漏吗?

任何帮助将不胜感激。

【问题讨论】:

    标签: amazon-web-services amazon-s3 aws-sdk aws-sdk-net


    【解决方案1】:

    我怀疑 AWS .NET SDK 存在一些问题,特别是对 S3 的异步调用。

    对 dynamoDB 的异步调用完美运行,但 S3 永远挂起。

    解决我的问题的方法是简单地删除异步功能(即使在 AWS 文档中,也应该使用异步调用)

    之前:

    using (GetObjectResponse response = await client.GetObjectAsync(request))
    

    之后:

    using (GetObjectResponse response =  myClient.GetObject(request))
    

    希望这可以帮助其他遇到此问题的人。

    【讨论】:

    • 使用最新的 SDK,一切都是异步的。没有同步对应物。
    猜你喜欢
    • 2019-03-18
    • 2021-08-30
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2018-11-22
    相关资源
    最近更新 更多