2013-09-05 54 views
6

Tôi đang trong quá trình tải hình ảnh lên Amazon S3, tuy nhiên tôi vẫn gặp lỗi "Vui lòng chỉ định Tên tệp, cung cấp FileStream hoặc cung cấp ContentBody để PUT một đối tượng S3. "ASP.NET tải tệp lên Amazon S3

Về cơ bản tôi đang tải lên hình ảnh từ điều khiển tải tệp lên rồi nhấn vào mã bên dưới. Nó tải lên cục bộ tốt, nhưng không tải lên Amazon. Các thông tin đăng nhập là alright vì vậy nó chỉ lỗi khi nói đến uplaoding.

Có ai có thể biết tại sao điều này xảy ra không?

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

Trả lời

10

Bạn cần gán thuộc tính Tệp hoặc InputStream của đối tượng PutObjectRequest. Đoạn mã sẽ trông giống như sau:

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

Cảm ơn Alexandr rất nhiều! Làm việc một điều trị. Bây giờ jsut cần phải làm việc ra làm thế nào để giảm kích thước tập tin: D Và hy vọng điều này sẽ giúp người khác. – thatuxguy

+0

liên kết dưới đây có thể giúp giảm kích thước hình ảnh mà không làm mất chất lượng của nó. http://www.aspdotnet-suresh.com/2011/05/how-to-resize-size-image-without-losing.html – mathewtinuthomas

Các vấn đề liên quan