如何从Amazon Web Services S3存储桶中的特定文件夹中下载文件。
到目前为止,我正在android studio中导入amazon transfer Utility项目示例,它工作正常,没有问题,但是每当我从AWS-S3存储桶下载文件时,该文件就会下载到默认的外部存储位置。因此,我希望将文件下载到特定的文件夹中,例如storage / folder_name / file_name.txt。
试试这个..我正在我的项目中使用它,可以将图像下载到特定的文件夹..
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){
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句