AWS开发工具包.NET的问题

时间:2018-08-03 16:13:29

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

我正在尝试从存储桶中检索图像以发送到我的移动应用程序,当前我的设备可以直接访问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)) 

这是在我不提供凭据时引发凭据错误的同一行。这里有我想念的东西吗?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

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

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

之前:

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

之后:

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

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

相关问题