ASP.NET上传文件到Amazon S3

7
我正在上传图片到Amazon S3过程中,但是我一直得到错误信息:“请指定文件名、提供文件流或提供内容正文以将对象放入S3。”。基本上,我正在从fileupload控件上传图像,然后点击以下代码。它可以在本地上传成功,但无法上传到亚马逊。凭证没有问题,所以只有在上传时才会出错。请问有人能看出这是为什么吗?
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";
        }
    }
1个回答

12
您需要为PutObjectRequest对象分配File或InputStream属性。代码片段应该像这样:

  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);
                    }

非常感谢你,亚历山大!非常好用。现在只需要解决如何减小文件大小的问题:D 希望这对其他人有帮助。 - thatuxguy
以下链接可能有助于缩小图像大小而不失去其质量。 http://www.aspdotnet-suresh.com/2011/05/how-to-resize-size-image-without-losing.html - Tinu Mathew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接