2016-04-30 216 views
1

如何從特定文件夾中的Amazon Web Services S3存儲桶下載文件。Android亞馬遜s3存儲桶

截至目前,我在我的android工作室導入amazon傳輸實用程序項目示例,它的工作正常沒有問題,但每當我從AWS-S3下載文件時,將文件下載到默認外部存儲位置。 因此,我希望該文件將被下載到特定的文件夾中,例如storage/folder_name/file_name.txt

回答

1

試試這個..我在我的項目能夠對圖像下載到特定文件夾中使用這個..

File imageImage = new File(mContext.getDir(USER_PROFILE_DIRECTORY, Context.MODE_PRIVATE)+ ImageName).getAbsoluteFile(); 
Download(checkImage,Constants.AMAZON_BUCKET_IMAGE,ImageName); 

/** 
* To download the image from the Amazon S3 bucket and store the image locally to the particular directory 
* @param DownloadingImagePath Source path of the image file to be stored 
* @param ImageName Image file name/Object key of Amazon S3 
* @param ImageBucket Amazon S3 bucket name 
*/ 
public void Download(File DownloadingImagePath,String ImageBucket,String ImageName){ 
try{ 
     AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY); 
     AmazonS3 s3 = new AmazonS3Client(credentials); 
       java.security.Security.setProperty("networkaddress.cache.ttl" , "60"); 
       s3.setRegion(Region.getRegion(Regions.AP_SOUTHEAST_1)); 
       s3.setEndpoint("https://s3-ap-southeast-1.amazonaws.com/"); 
       TransferUtility transferUtility = new TransferUtility(s3, mContext); 
       TransferObserver observer = transferUtility.download(ImageBucket, ImageName, DownloadingImagePath); 
       observer.setTransferListener(new TransferListener() { 
        @Override 
        public void onStateChanged(int id, TransferState state) { 
         //Log.e("Amazon Stats",state.name()); 
        } 

        @Override 
        public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) { 

        } 

        @Override 
        public void onError(int id, Exception ex) { 

        } 
       }); 
       } catch (Exception ignored){ 
      } 
     } 
+0

找你的歡迎。請如果這個答案幫助你,請接受它。它可以幫助其他人解決如何尋找相同..享受編碼......... –

+0

你好安丹,..我能夠做到這一點,你建議我..一個更多的問題...我有這麼多的文件在S3桶,但我只下載那些我點擊的文件...所以,我想從S3桶中下載所有的文件在我loacal /外部存儲.....任何進一步的建議相當多.. –

+0

我的圖像保存在存儲桶中的文件夾中,所以我必須提及該文件夾作爲存儲桶名稱或在密鑰中提及它? –

1

您可以使用AWS SDK將文件下載到任何您想要的位置。 或者使用CLI工具,如AWS CLIs3cmd

+0

非常感謝西蒙 –