7

我正在将图像上传到 Amazon S3,但是我不断收到错误消息“请指定文件名、提供 FileStream 或提供 ContentBody 以将对象放入 S3。”

基本上我正在从文件上传控件上传图像,然后点击下面的代码。它可以在本地上传,但不能上传到亚马逊。凭证没问题,所以它只会在上传时出错。

谁能明白为什么会这样?

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";
        }
    }
4

1 回答 1

11

您需要分配 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);
                    }
于 2013-09-05T12:27:09.897 回答