2013-09-05 5 views
6

पर एक फाइल अपलोड करने के लिए मैं अमेज़ॅन एस 3 पर छवियों को अपलोड करने की प्रक्रिया में हूं, हालांकि मुझे त्रुटि त्रुटि मिल रही है "कृपया फ़ाइल नाम निर्दिष्ट करें, फ़ाइलस्ट्रीम प्रदान करें या किसी ऑब्जेक्ट को पुट करने के लिए ContentBody प्रदान करें S3। "एएसपी.NET अमेज़ॅन एस 3

असल में मैं एक फ़ाइल अपलोड नियंत्रण से एक छवि अपलोड कर रहा हूं और फिर नीचे दिए गए कोड को मार रहा हूं। यह स्थानीय रूप से ठीक अपलोड करता है, लेकिन अमेज़ॅन के लिए नहीं। प्रमाण पत्र ठीक हैं, इसलिए जब यह अपलोडिंग की बात आती है तो यह केवल त्रुटियां होती है।

क्या कोई देख सकता है कि यह क्यों हो रहा है?

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

उत्तर

10

आप फ़ाइल या PutObjectRequest वस्तु की 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); 
        } 
+0

धन्यवाद इतना एलेक्ज़ेंडर! एक इलाज किया। अब jsut को फ़ाइल आकार को कम करने के तरीके को समझने की आवश्यकता है: डी और उम्मीद है कि इससे दूसरों की मदद मिलेगी। लिंक के नीचे – thatuxguy

+0

इसकी गुणवत्ता खोए बिना छवि आकार को कम करने में मदद कर सकता है। http://www.aspdotnet-suresh.com/2011/05/how-to-resize-size-image-without-losing.html – mathewtinuthomas