簡體   English   中英

使用來自IIS .Net的AWS S3 API時,對象引用未設置為對象的實例

[英]Object reference not set to an instance of an object when using AWS S3 API from IIS .Net ASP.Net

我正在嘗試將文件保存到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. 不同的鍵(嘗試單詞“等等”)
  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

這個問題都與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的錯誤非常嚴重。 正確的錯誤消息,我可以節省很多時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM