Amazon icon indicating copy to clipboard operation
Amazon copied to clipboard

Demo shows how to Put an Object to S3?

Open mirror222 opened this issue 5 years ago • 1 comments
trafficstars

I wrote a demo to upload a file to S3 with the following codes, it always gets the following error: S3Exception: The request signature we calculated does not match the signature you provided. Check your key and signing method.

i have double-checked my access keys.

            var cred = new Amazon.AwsCredential("xxxxx", "xxxxx");
            var client = new Amazon.S3.S3Client(new Amazon.AwsRegion("ap-northeast-1"), cred);
             var obj = new PutObjectRequest(client.Host, "backup", "/rewrite_amd64.msi");
            obj.SetStorageClass(StorageClass.ReducedRedundancy);
            var fs = new FileStream(@"C:\rewrite_amd64.msi", FileMode.Open);
            obj.SetStream(fs, "application/octet-stream");
            var mytask= client.PutObjectAsync(obj);
            mytask.Wait();
            Console.WriteLine(mytask.Result);

thank you.

mirror222 avatar Mar 24 '20 14:03 mirror222

Your example helped me, but make sure the arguments are correct. you have /rewrite_amd64.msi, which should be trimmed from slashes...

 using var obj = new PutObjectRequest(_client.Host, builder.Host, $"{builder.Path.TrimStart('/')}/{fileName}");
           obj.SetStorageClass(StorageClass.StandardInfrequentAccess);
           using var sourceStream = File.OpenRead(source);
           obj.SetStream(sourceStream, contentType);
           var result = await _client.PutObjectAsync(obj, cancellationToken);

egbertn avatar Nov 10 '21 14:11 egbertn