Goal:
Sample code for "Upload an Object Using the AWS SDK for Java"Env:
aws-java-sdk-1.7.4.jarCentOS 6
Solution:
Say I want to upload one file "/root/hao/abc.txt" from CentOS node to S3 location "s3://haos3/test/a.txt". ("hao3" is the bucket name).1. Create AWS Credentials File : ~/.aws/config
The current user is root, so the location for this file is /root/.aws/config.The content is Access Key and Secret Access Key.
For example:
1 2 3 4 | # cat /root/.aws/config [default] aws_access_key_id=AxxxxxA aws_secret_access_key=axxxxxxxxxxxxxxxxxxxxa |
2. Create sample java code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import java.io.File; import java.io.IOException; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.PutObjectRequest; import com.amazonaws.services.s3.model.ObjectMetadata; public class UploadObjectSingleOperation { private static String bucketName = "haos3" ; private static String keyName = "test/a.txt" ; private static String uploadFileName = "/root/hao/abc.txt" ; public static void main(String[] args) throws IOException { AmazonS3 s3client = new AmazonS3Client( new ProfileCredentialsProvider()); try { System.out.println( "Uploading a new object to S3 from a file\n" ); File file = new File(uploadFileName); PutObjectRequest putRequest = new PutObjectRequest(bucketName, keyName, file); // Request server-side encryption. // ObjectMetadata objectMetadata = new ObjectMetadata(); // objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); // putRequest.setMetadata(objectMetadata); s3client.putObject(putRequest); } catch (AmazonServiceException ase) { System.out.println( "Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason." ); System.out.println( "Error Message: " + ase.getMessage()); System.out.println( "HTTP Status Code: " + ase.getStatusCode()); System.out.println( "AWS Error Code: " + ase.getErrorCode()); System.out.println( "Error Type: " + ase.getErrorType()); System.out.println( "Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println( "Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network." ); System.out.println( "Error Message: " + ace.getMessage()); } } } |
3. Compile it with needed libs
1 2 | export CLASSPATH= /opt/mapr/hadoop/hadoop-2 .7.0 /share/hadoop/tools/lib/aws-java-sdk-1 .7.4.jar:.: /opt/mapr/hadoop/hadoop-2 .7.0 /share/hadoop/common/lib/ * javac UploadObjectSingleOperation.java |
4. Test
1 2 3 4 5 | # java UploadObjectSingleOperation log4j:WARN No appenders could be found for logger (com.amazonaws.internal.config.InternalConfig). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http: //logging .apache.org /log4j/1 .2 /faq .html #noconfig for more info. Uploading a new object to S3 from a file |
Reference:
http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.htmlhttp://docs.aws.amazon.com/AmazonS3/latest/dev/SSEUsingJavaSDK.html
c
ReplyDeleteBEF2C89CD2
ReplyDeleteen çok oynanan mmorpg oyunlar
sms onay sitesi
güvenilir mobil ödeme bozdurma forum
güvenilir takipçi satın alma instagram
sahte takipçi