【发布时间】: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 调用中尝试了一些方法:
- 不同的键(试过'blah'这个词)
- 不同的桶
- 发送 文件作为路径而不是流
因为我真的只提供 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