public static String uploadFile(File file, AWSCredentials credentials,
String bucketName, String keyForFileInAWS) {
// 创建S3客户端对象
AmazonS3 s3 = new AmazonS3Client(credentials);
Region region = Region.getRegion(BaseConstant.REGION);
s3.setRegion(region);
// 若不存在存储桶,则创建
if (!isBucketExists(s3, bucketName)) { }
// 上传文件
s3.putObject(new PutObjectRequest(bucketName, keyForFileInAWS, file));
}
其中AWSCredentials credentials及AmazonS3 s3 哪些是支持多线程的?
String bucketName, String keyForFileInAWS) {
// 创建S3客户端对象
AmazonS3 s3 = new AmazonS3Client(credentials);
Region region = Region.getRegion(BaseConstant.REGION);
s3.setRegion(region);
// 若不存在存储桶,则创建
if (!isBucketExists(s3, bucketName)) { }
// 上传文件
s3.putObject(new PutObjectRequest(bucketName, keyForFileInAWS, file));
}
其中AWSCredentials credentials及AmazonS3 s3 哪些是支持多线程的?
解决方案 »
- 关于openstack swift组件问题 求知道
- devstack有图形界面么,怎样启动呢?
- 如何隔绝不同项目之的网络,如何限制vm公网出口的带宽?
- XP 命令行连接无线网怎么做 ?
- 一个大量科学计算的项目用什么云?
- hadoop2.7.0 统计字符数量,报“The auxService:mapreduce_shuffle does not exist”异常
- 使用collect函数时出现java.lang.ClassCastException: cannot assign instance of
- 桌面云虚拟化和服务器虚拟化区别
- docker pull 私有仓库 失败,求docker高手帮忙
- docker使用overlay网络模式,为什么主机ping不通本机的容器,容器里ping跨主机容器ip、所有宿主机ip都正常
- 批发服务器CPU如下
- sae求大神帮我看看新浪云抽的是啥疯?
PropertiesUtil propertiesUtil = new PropertiesUtil("s3.properties");
//首先创建一个s3的客户端操作对象(需要amazon提供的密钥)
AmazonS3 s3 = new AmazonS3Client(
new BasicAWSCredentials(propertiesUtil.getKeyValue(Consts.S3_ACCESS_KEY),
propertiesUtil.getKeyValue(Consts.S3_SCERET_KEY)));
Region usWest2 = Region.getRegion(Regions.US_WEST_2);
s3.setRegion(usWest2);
//设置bucket,key
String bucketName = Consts.S3_BUCKET_NAME;
String key = UUID.randomUUID() + ".apk";
try {
//验证名称为bucketName的bucket是否存在,不存在则创建
if (!checkBucketExists(s3, bucketName)) {
s3.createBucket(bucketName);
}
//上传文件
s3.putObject(new PutObjectRequest(bucketName, key, tempFile));
S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
//获取一个request
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(
bucketName, key);
Date expirationDate = null;
try {
expirationDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-12-31");
} catch (Exception e) {
e.printStackTrace();
}
//设置过期时间
urlRequest.setExpiration(expirationDate);
//生成公用的url
URL url = s3.generatePresignedUrl(urlRequest);
System.out.println("=========URL=================" + url + "============URL=============");
if (url == null) {
throw new OperateFailureException("can't get s3 file url!");
}
return url.toString();
} catch (AmazonServiceException ase) {
ase.printStackTrace();
logger.info("====================================AWS S3 UPLOAD ERROR START======================================");
logger.info("Caught an AmazonServiceException, which means your request made it "
+ "to Amazon S3, but was rejected with an error response for some reason.");
logger.info("Caught an AmazonServiceException, which means your request made it "
+ "to Amazon S3, but was rejected with an error response for some reason.");
logger.info("Error Message: " + ase.getMessage());
logger.info("HTTP Status Code: " + ase.getStatusCode());
logger.info("AWS Error Code: " + ase.getErrorCode());
logger.info("Error Type: " + ase.getErrorType());
logger.info("Request ID: " + ase.getRequestId());
logger.info(ase.getMessage(), ase);
logger.info("====================================AWS S3 UPLOAD ERROR END======================================");
throw new OperateFailureException("error occurs during upload to s3!");
} catch (AmazonClientException ace) {
logger.info("====================================AWS S3 UPLOAD ERROR START======================================");
logger.info("Caught an AmazonClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with S3, "
+ "such as not being able to access the network.");
logger.info("Error Message: " + ace.getMessage());
logger.info("====================================AWS S3 UPLOAD ERROR END======================================");
throw new OperateFailureException("error occurs during upload to s3!");
}
} /**
* 验证s3上是否存在名称为bucketName的Bucket
* @param s3
* @param bucketName
* @return
*/
public static boolean checkBucketExists (AmazonS3 s3, String bucketName) {
List<Bucket> buckets = s3.listBuckets();
for (Bucket bucket : buckets) {
if (Objects.equals(bucket.getName(), bucketName)) {
return true;
}
}
return false;
}
我也找到这段代码了。我想问的是在web开发中是否可以将BasicAWSCredentials credentials(即new BasicAWSCredentials(propertiesUtil.getKeyValue(Consts.S3_ACCESS_KEY),
propertiesUtil.getKeyValue(Consts.S3_SCERET_KEY))这个对象做成单例。