繁体   English   中英

ASP.NET 将文件上传到 Amazon S3

[英]ASP.NET uploading a file to Amazon S3

我正在将图像上传到 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";
        }
    }

您需要分配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);
                    }

一次只能设置这两个(FilePath 或 InputStream)之一

"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM