ASP.NET uploads file to Amazon S3

I upload images to Amazon S3, but I still get an error message . Please provide either a file name or a FileStream file, or provide a ContentBody to place the object on S3.

I basically load the image from the file system control and then click on the code below. It loads locally well, but not on Amazon. Accounts are fine, so these are only errors when it comes to uplaoding.

Can anyone understand why this is happening?

protected void uploadImg(int prodId, int prodFormat) { if (imgPack.HasFile) { string fileExt = Path.GetExtension(imgPack.PostedFile.FileName); string filename = "img" + prodId + ".jpg"; // Specify the upload directory string directory = Server.MapPath(@"\images\packshots\"); if (fileExt == ".jpeg" || fileExt == ".jpg" || fileExt == ".png") { if (packUK.PostedFile.ContentLength < 716800) { // Create a bitmap of the content of the fileUpload control in memory Bitmap originalBMP = new Bitmap(packUK.FileContent); // Calculate the new image dimensions decimal origWidth = originalBMP.Width; decimal origHeight = originalBMP.Height; decimal sngRatio = origHeight / origWidth; int newHeight = 354; //hight in pixels decimal newWidth_temp = newHeight / sngRatio; int newWidth = Convert.ToInt16(newWidth_temp); // Create a new bitmap which will hold the previous resized bitmap Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight); // Create a graphic based on the new bitmap Graphics oGraphics = Graphics.FromImage(newBMP); // Set the properties for the new graphic file oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // Draw the new graphic based on the resized bitmap oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight); // Save the new graphic file to the server string accessKey = "KEY HERE"; string secretKey = "KEY HERE"; AmazonS3 client; using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey)) { PutObjectRequest request = new PutObjectRequest(); request.BucketName="MyBucket"; request.CannedACL = S3CannedACL.PublicRead; request.Key = "images/" + filename; S3Response response = client.PutObject(request); } //newBMP.Save(directory + filename); // Once finished with the bitmap objects, we deallocate them. originalBMP.Dispose(); newBMP.Dispose(); oGraphics.Dispose(); } } else { notifybar.Attributes.Add("style", "display:block;"); notifybar.Attributes.Add("class", "failed"); notifyText.Text = "Error Text Here"; } } else { notifybar.Attributes.Add("style", "display:block;"); notifybar.Attributes.Add("class", "failed"); notifyText.Text = "Error Text Here"; } } 
+6
source share
1 answer

You need to assign the File or InputStream property of the PutObjectRequest object. The code fragment should look like this:

  using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey)) { var stream = new System.IO.MemoryStream(); originalBMP.Save(stream, ImageFormat.Bmp); stream.Position = 0; PutObjectRequest request = new PutObjectRequest(); request.InputStream = stream; request.BucketName="MyBucket"; request.CannedACL = S3CannedACL.PublicRead; request.Key = "images/" + filename; S3Response response = client.PutObject(request); } 
+10
source

Source: https://habr.com/ru/post/953237/


All Articles