【问题标题】:Object reference not set to an instance of an object when using AWS S3 API from IIS .Net ASP.Net从 IIS .Net ASP.Net 使用 AWS S3 API 时,对象引用未设置为对象的实例
【发布时间】:2019-06-19 07:29:42
【问题描述】:

我正在尝试将文件保存到 S3 存储桶。我将文件复制到临时位置只是为了证明(对我而言)文件存在并且可以访问。当然,副本没有问题。

最后一行不断抛出异常,详细信息说“对象引用未设置为对象的实例”。

File.Copy(fullFilePath, "C:\\Temp\\" + fullFileName);

IAmazonS3 client = null;
if (Session["s3Client"] != null) {
    client = (IAmazonS3)Session["s3Client"];
} else {
    client = new AmazonS3Client(RegionEndpoint.GetBySystemName(AWS_REGION));
} // if

PutObjectRequest putRequest = new PutObjectRequest {
    BucketName = S3_BUCKET_NAME,
    Key = fullFileName
};

using (FileStream stream = new FileStream(fullFilePath, FileMode.Open)) {
    putRequest.InputStream = stream;

    // Put object
    PutObjectResponse response = client.PutObject(putRequest);
}

我在 S3 调用中尝试了一些方法:

  1. 不同的键(试过'blah'这个词)
  2. 不同的桶
  3. 发送 文件作为路径而不是流

因为我真的只提供 3 个字段,所以我没有太多可以尝试的。

我试图将身份验证留在代码之外,以便主机可以控制它。

有什么想法吗?

非常感谢。

编辑:

at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.FetchCredentials()
at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentials()
at Amazon.Runtime.Internal.CredentialsRetriever.PreInvoke(IExecutionContext executionContext)
at Amazon.Runtime.Internal.CredentialsRetriever.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.RetryHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.CallbackHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.CallbackHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.S3.Internal.AmazonS3ExceptionHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.MetricsHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.RuntimePipeline.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.AmazonServiceClient.Invoke[TResponse](AmazonWebServiceRequest request, InvokeOptionsBase options)
at MyCode.aspx.cs:line 260

【问题讨论】:

  • 请检查异常的堆栈跟踪以确定引发异常的确切位置
  • 只是一个 c# 观察...如果 putRequest.InputStream = stream; 没有抛出异常,那么 null 必须是客户端。
  • ...而您尝试过的任何事情都不会对此做任何任何事情 ;-)
  • @GaretJax:使用任何反编译器(IlSpy 可以)反编译他们的 API 并查看实现。检查他们如何获取凭据以及您可以做些什么。它甚至可能是一些简单的事情(例如,您错过了正确的配置键/值)。
  • 堆栈跟踪有帮助 ;-)

标签: c# .net amazon-web-services amazon-s3


【解决方案1】:

这个问题都与 IIS 权限有关。 AWS 试图通过 web.config 文件中的条目访问凭证:

<configSections>
  <section name="aws" type="Amazon.AWSSection, AWSSDK.Core"/>
</configSections>
<aws profileName="default" profilesLocation="C:\Users\Administrator\.aws\credentials"/>

当 IIS 运行时,它无权访问上面列出的凭据文件。

解决方案是授予 defaultAppPool 对文件的读取权限。我通过将“IIS APPPOOL\defaultAppPool”添加到目录的安全选项卡来确保它具有读取权限。

来自 AWS 的错误非常严重。正确的错误消息,我会节省很多时间。

【讨论】:

    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多