C# ASP.NET将文件上载到Amazon S3

C# ASP.NET将文件上载到Amazon S3,c#,asp.net,file-upload,amazon-web-services,amazon-s3,C#,Asp.net,File Upload,Amazon Web Services,Amazon S3,我正在将图像上载到Amazon S3,但是我不断收到错误“请指定文件名、提供文件流或提供ContentBody以将对象放入S3。” 基本上我是从fileupload控件上传一个图像,然后点击下面的代码。它可以在本地上传,但不能上传到亚马逊。凭据是正常的,所以它只是在上传时出错 有人能明白为什么会这样吗 protected void uploadImg(int prodId, int prodFormat) { if (imgPack.HasFile) {

我正在将图像上载到Amazon S3,但是我不断收到错误“请指定文件名、提供文件流或提供ContentBody以将对象放入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";
        }
    }
protectedvoiduploadimg(int-prodId,int-prodFormat)
{
if(imgPack.HasFile)
{
字符串fileExt=Path.GetExtension(imgPack.PostedFile.FileName);
字符串filename=“img”+prodId+”.jpg”;
//指定上载目录
string directory=Server.MapPath(@“\images\packshots\”);
如果(fileExt==”.jpeg“| | fileExt==”.jpg“| | fileExt==”.png”)
{
if(packUK.PostedFile.ContentLength<716800)
{
//在内存中创建fileUpload控件内容的位图
位图originalBMP=新位图(packUK.FileContent);
//计算新的图像尺寸
十进制origWidth=原始bmp.宽度;
十进制原点高度=原点高度;
十进制sngRatio=初始高度/初始宽度;
int newHeight=354;//以像素为单位的高度
十进制新宽度=新高度/零度;
int newWidth=Convert.ToInt16(newWidth\u temp);
//创建一个新位图,该位图将保存以前调整大小的位图
位图newBMP=新位图(原始BMP、新宽度、新高度);
//基于新位图创建图形
Graphics oGraphics=Graphics.FromImage(newBMP);
//设置新图形文件的属性
oGraphics.SmoothingMode=SmoothingMode.AntiAlias;
oGraphics.InterpolationMode=InterpolationMode.HighQualityBicubic;
//根据调整大小的位图绘制新图形
Graphics.DrawImage(原始BMP,0,0,新宽度,新高度);
//将新图形文件保存到服务器
string accessKey=“KEY HERE”;
string secretKey=“KEY HERE”;
AmazonS3客户端;
使用(client=Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey,secretKey))
{
PutObjectRequest=新建PutObjectRequest();
request.BucketName=“MyBucket”;
request.CannedACL=S3CannedACL.PublicRead;
request.Key=“images/”+文件名;
S3Response=client.PutObject(请求);
}
//newBMP.Save(目录+文件名);
//完成位图对象后,我们将释放它们。
原始bmp.Dispose();
newBMP.Dispose();
Dispose();
}
}
其他的
{
添加(“样式”,“显示:块;”);
Add(“class”,“failed”);
notifyText.Text=“此处的错误文本”;
}
}
其他的
{
添加(“样式”,“显示:块;”);
Add(“class”,“failed”);
notifyText.Text=“此处的错误文本”;
}
}

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

非常感谢Alexandr!工作很愉快。现在jsut需要解决如何减少文件大小:D,希望这将有助于其他人。下面的链接可能有助于在不降低图像质量的情况下减少图像大小。