Home > other >  AWS S3 upload (SSH framework which supports multi-threading is the object?
AWS S3 upload (SSH framework which supports multi-threading is the object?

Time:09-27

Public static String uploadFile (File File, AWSCredentials credentials,
String bucketName, String keyForFileInAWS) {
//create the S3 client object
AmazonS3 s3=new AmazonS3Client (credentials);
Region Region=Region. GetRegion (BaseConstant. Region);
S3. SetRegion (region);
//if there is no bucket, then create
if (! IsBucketExists (s3, bucketName)) {

}
//upload file
S3. PutObject (new PutObjectRequest (bucketName keyForFileInAWS, file));
}
The AWSCredentials credentials and which is to support multithreaded AmazonS3 s3?

CodePudding user response:

 public static String uploadToS3 (File tempFile as expected, the String remoteFileName) throws IOException {
PropertiesUtil PropertiesUtil=new PropertiesUtil (" s3. The properties ");
//first create a client operation of s3 objects (need to amazon provides key)
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);
//set the bucket, key
String bucketName=Consts. S3_BUCKET_NAME;
The String key=UUID. RandomUUID () + ". Apk ";
Try {
//verification name for the existence of the bucket and bucketName does not exist, create
if (! CheckBucketExists (s3, bucketName)) {
S3. CreateBucket (bucketName);
}
//upload file
S3. PutObject (new PutObjectRequest (bucketName, key, tempFile as expected));
S3Object object=s3. GetObject (new GetObjectRequest (bucketName, key));
//get a request
GeneratePresignedUrlRequest urlRequest=new GeneratePresignedUrlRequest (
BucketName, key);
The Date expirationDate=null;
Try {
ExpirationDate=new SimpleDateFormat (" yyyy - MM - dd "). The parse (" 2020-12-31 ");
} the catch (Exception e) {
e.printStackTrace();
}
//set the expiration time
UrlRequest. SetExpiration (expirationDate);
//generated public url
URL the URL=s3. GeneratePresignedUrl (urlRequest);
System. The out. Println ("=========URL================="+ URL +"============URL=============");
If (url==null) {
Throw new OperateFailureException (" can 't get s3 file url!" );
}
The return url. The toString ();
{} the catch (AmazonServiceException ase)
Ase. PrintStackTrace ();
Logger. The info ("====================================AWS S3 UPLOAD ERROR START======================================");
Logger. The info (" Caught an AmazonServiceException, which means your request made it "
+ "to Amazon S3, but was rejected with an error response for some reason.");
Logger. The info (" Caught an AmazonServiceException, which means your request made it "
+ "to Amazon S3, but was rejected with an error response for some reason.");
Logger. The info (" Error Message: "+ ase. GetMessage ());
Logger. The info (" HTTP Status Code: "+ ase. GetStatusCode ());
Logger. The info (" AWS Error Code: "+ ase. GetErrorCode ());
Logger. The info (" Error "Type:" + ase. GetErrorType ());
Logger. The info (" is the Request ID: "+ ase. GetRequestId ());
Logger. The info (ase. GetMessage (), ase);
Logger. The info ("====================================AWS S3 UPLOAD ERROR END======================================");
Throw new OperateFailureException (" error occurs during the upload to s3!" );
{} the catch (AmazonClientException ace)
Logger. The info ("====================================AWS S3 UPLOAD ERROR START======================================");
Logger. The info (" Caught an AmazonClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with S3,"
+ "to the as not being able to access the network.");
Logger. The info (" Error Message: "+ ace. The getMessage ());
Logger. The info ("====================================AWS S3 UPLOAD ERROR END======================================");
Throw new OperateFailureException (" error occurs during the upload to s3!" );
}
}

/* *
* verify whether there is a name on the s3 Bucket for bucketName
* @ param s3
* @ param bucketName
* @ return
*/
Public static Boolean checkBucketExists (AmazonS3 s3, String bucketName) {
List Buckets=s3. ListBuckets ();
For (Bucket Bucket: buckets) {
If (Objects) equals (bucket. GetName (), bucketName)) {
return true;
}
}
return false;
}

CodePudding user response:

reference 1st floor xmt1139057136 response:
 public static String uploadToS3 (File tempFile as expected, the String remoteFileName) throws IOException {
PropertiesUtil PropertiesUtil=new PropertiesUtil (" s3. The properties ");
//first create a client operation of s3 objects (need to amazon provides key)
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);
//set the bucket, key
String bucketName=Consts. S3_BUCKET_NAME;
The String key=UUID. RandomUUID () + ". Apk ";
Try {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • AWS
  • Related