Skip to content

Instantly share code, notes, and snippets.

@jriguera
Created April 28, 2015 14:32
Show Gist options
  • Save jriguera/1ac57af37f09fafd750d to your computer and use it in GitHub Desktop.
Save jriguera/1ac57af37f09fafd750d to your computer and use it in GitHub Desktop.
S3 test in java
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.List;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.util.StringUtils;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
public class UploadObjectSingleOperation {
private static String bucketName = "jriguera";
private static String accessKey = "";
private static String secretKey = "";
private static String url = "s4.test.springer-sbm.com";
public static void main(String[] args) throws IOException {
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
      S3ClientOptions s3ClientOptions = new S3ClientOptions();
       s3ClientOptions.setPathStyleAccess(true);
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setProtocol(Protocol.HTTP);
AmazonS3 s3client = new AmazonS3Client(credentials, clientConfig);
s3client.setS3ClientOptions(s3ClientOptions);
s3client.setEndpoint(url);
try {
System.out.println("Creating a new bucket\n");
s3client.createBucket(bucketName);
System.out.println("Buckets\n");
for (Bucket bucket : s3client.listBuckets()) {
System.out.println(" - " + bucket.getName());
} 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());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment