Reputation: 1557
I'm working on uploading local files to aws s3 in C# .net, and I'm using putObjectRequest
to make the request to aws s3, however, I've just received a thrown exception: Invalid URI: The format of the URI could not be determined.
. Here is what I'm doing:
try {
string testString = @"C:\Path\to\object files\file.jpg";
PutObjectRequest objReq = new PutObjectRequest
{
Key = key,
ContentType = "JPG",
BucketName = bucket + "/" + FolderName,
CannedACL = S3CannedACL.PublicReadWrite,
FilePath = testString
};
PutObjectResponse response = s3Client.PutObject(objReq);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
I carefully checked the BucketName
, RegionEndPoint
and ServiceURL
, they have been set correctly, so I'm thinking the FilePath
is not correct. However, after searching the file path format in C#, I think I'm using the correct operator to escape the backslash. Is there any other cause can lead to this exception?
The entire stackTrace is like this:
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Uri..ctor(String uriString)
at Amazon.Runtime.Internal.EndpointResolver.DetermineEndpoint(IClientConfig config, IRequest request)
at Amazon.S3.Internal.AmazonS3PostMarshallHandler.ProcessRequestHandlers(IExecutionContext executionContext)
at Amazon.S3.Internal.AmazonS3PostMarshallHandler.PreInvoke(IExecutionContext executionContext)
at Amazon.S3.Internal.AmazonS3PostMarshallHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.PipelineHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.Marshaller.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.PipelineHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.S3.Internal.AmazonS3PreMarshallHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.PipelineHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.CallbackHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.PipelineHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.S3.Internal.AmazonS3ExceptionHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.PipelineHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.PipelineHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.MetricsHandler.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.Internal.RuntimePipeline.InvokeSync(IExecutionContext executionContext)
at Amazon.Runtime.AmazonServiceClient.Invoke[TRequest,TResponse](TRequest request, IMarshaller`2 marshaller, ResponseUnmarshaller unmarshaller)
at Amazon.S3.AmazonS3Client.PutObject(PutObjectRequest request)
at Camera_Capture.UploadFileToAmazonS3(String Filepath, String camstring, String SerialNumber) in C:\path\to\Program.cs:line 371
Upvotes: 0
Views: 5616
Reputation: 1557
It turns out to be a very misleading error message. The problem is from AmazonS3Client(accesskey, secretkey, AmazonS3Config config);
was not getting the RegionEndpoint
when AmazonS3Config
was constructed. I changed the S3 client constructor to AmazonS3Client(accesskey, secretkey, RegionEndpoint.USWest2);
and it resolved the error for me. I think Amazon itself should return a message of RegionEndpoint of config is null
rather than this misleading message Invalid URI: The format of the URI could not be determined
that .net will throw, because it can be caused by many other reasons.
Upvotes: 1