Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET uploading a file to Amazon S3

I am in the process of uploading images to Amazon S3, however i keep getting the error "Please specify either a Filename, provide a FileStream or provide a ContentBody to PUT an object into S3."

Basically i am uploading an image from a fileupload control and then hitting the code below. It uploads locally fine, but not to Amazon. The Credentials are alright so it only errors when it comes to uplaoding.

Can anyone see why this is happening please?

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";
        }
    }
like image 778
thatuxguy Avatar asked Sep 05 '13 12:09

thatuxguy


People also ask

How do I upload files to Amazon S3?

In the Amazon S3 console, choose the bucket where you want to upload an object, choose Upload, and then choose Add Files. In the file selection dialog box, find the file that you want to upload, choose it, choose Open, and then choose Start Upload. You can watch the progress of the upload in the Transfer pane.

Can we upload CSV file to S3 bucket?

Upload your CSV files to an Amazon Simple Storage Service ( Amazon S3) bucket. This is the location that Amazon Personalize imports your data from. For more information, see Uploading Files and Folders by Using Drag and Drop in the Amazon Simple Storage Service User Guide.

Can we upload multiple files to S3?

Upload multiple files to AWS CloudShell using Amazon S3. Next, you need to upload the files in a directory from your local machine to the bucket. You have two options for uploading files: AWS Management Console: Use drag-and-drop to upload files and folders to a bucket.


1 Answers

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

  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);
                    }
like image 100
Alexandr Mihalciuc Avatar answered Sep 23 '22 11:09

Alexandr Mihalciuc