2017-03-27 23 views
1

AWS S3を使用してWebアプリケーションを構築しています。S3ファイルをダウンロード

質問:質問:S3からファイルをダウンロードし、そのファイルをユーザーのダウンロードフォルダに保存するにはどうすればよいですか?これまで

私のコードは:

this.getS3().getObject({Bucket:bucket_name, Key:keys}, 
     function(error, data) { 
     if (error) { 
      console.log("Error!") 
     } else { 
      // How to save 'data' file in Download folder? 
     } 
     }); 
+0

[これ](http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/requests-using-stream-objects.html)を試しましたか? – franklinsijo

答えて

0

これは私がbyte[]を返すメソッドGetFileObjectにパラメータを渡します。ネット

public class AmazonS3 
    { 
     //Local varaibles to get the accesskey and secretkey 
     private readonly string accessKey; 
     private readonly string secretKey; 
     private RegionEndpoint regionEndpoint; 
     AmazonS3Client client = null; 
     TransferUtility utility = null; 

     /// <summary> 
     /// Class Constructor for getting the accesskey and secretkey for the Amazon S3 bucket 
     /// </summary> 
     public AmazonS3Operations() 
     { 
      //get the accesskey and secretkey from the web.config 

      accessKey = ConfigurationManager.AppSettings["AWSS3AccessKey"]; 
      secretKey = ConfigurationManager.AppSettings["AWSS3SecretKey"]; 
      regionEndpoint = RegionEndpoint.GetBySystemName(ConfigurationManager.AppSettings["AWSS3RegionEndPoint"]); 

      //Initialize the connection here and use it every where in a singleton model 
      client = GetS3ClientConnection(); 
     } 

public byte[] GetFileObject(string filePath, string fileNameInS3, out string strContentType) 
     { 
      byte[] data = null; 

      GetObjectRequest request = new GetObjectRequest(); 
      request.BucketName = filePath; 
      request.Key = fileNameInS3; 

      //get the object response here 
      GetObjectResponse response = client.GetObject(request); 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       response.ResponseStream.CopyTo(ms); 
       data = ms.ToArray(); 
      } 
      //get the Mimetype here     
      strContentType = MimeMapping.GetMimeMapping(fileNameInS3); 
      return data; 

     } 

} 

用AWS SDKを使用して.NETでやっている方法ですアレイは応答します。ユーザーのダウンロードフォルダにそれぞれの名前とMIMEタイプのファイルを作成します。

は、Javaで同じことを行うためにここに参照してください:http://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingJava.html

あまりにも他の言語のためのオンライン利用可能な他のAWSの文書があります。

関連する問題