List of usage examples for com.amazonaws.services.s3 AmazonS3 doesBucketExist
@Deprecated public boolean doesBucketExist(String bucketName) throws SdkClientException, AmazonServiceException;
From source file:com.amazon.util.ImageUploader.java
public static void uploadImage(String imageURL, String imageName, String folderName, String bucketName) throws MalformedURLException, IOException { // credentials object identifying user for authentication AWSCredentials credentials = new BasicAWSCredentials(System.getenv("AWS_S3_ACCESS_KEY"), System.getenv("AWS_S3_SECRET_ACCESS_KEY")); // create a client connection based on credentials AmazonS3 s3client = new AmazonS3Client(credentials); try {/*from ww w .j a v a2 s. c om*/ if (!(s3client.doesBucketExist(bucketName))) { s3client.setRegion(Region.getRegion(Regions.US_EAST_1)); // Note that CreateBucketRequest does not specify region. So bucket is // created in the region specified in the client. s3client.createBucket(new CreateBucketRequest(bucketName)); } //Enabe CORS: // <?xml version="1.0" encoding="UTF-8"?> //<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> // <CORSRule> // <AllowedOrigin>http://ask-ifr-download.s3.amazonaws.com</AllowedOrigin> // <AllowedMethod>GET</AllowedMethod> // </CORSRule> //</CORSConfiguration> BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration(); CORSRule corsRule = new CORSRule() .withAllowedMethods( Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.GET })) .withAllowedOrigins(Arrays.asList(new String[] { "http://ask-ifr-download.s3.amazonaws.com" })); configuration.setRules(Arrays.asList(new CORSRule[] { corsRule })); s3client.setBucketCrossOriginConfiguration(bucketName, configuration); } 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()); } String fileName = folderName + SUFFIX + imageName + ".png"; URL url = new URL(imageURL); ObjectMetadata omd = new ObjectMetadata(); omd.setContentType("image/png"); omd.setContentLength(url.openConnection().getContentLength()); // upload file to folder and set it to public s3client.putObject(new PutObjectRequest(bucketName, fileName, url.openStream(), omd) .withCannedAcl(CannedAccessControlList.PublicRead)); }
From source file:com.easarrive.aws.plugins.common.service.impl.S3Service.java
License:Open Source License
/** * {@inheritDoc}//from www . ja v a 2s .c o m */ @Override public Bucket createBucket(AmazonS3 client, String bucketName) { if (client == null) { return null; } else if (StringUtil.isEmpty(bucketName)) { return null; } Bucket bucket = null; if (!client.doesBucketExist(bucketName)) { bucket = client.createBucket(bucketName); } return bucket; }
From source file:com.easarrive.aws.plugins.common.service.impl.S3Service.java
License:Open Source License
/** * {@inheritDoc}/*from w ww .j a v a2s . c o m*/ */ @Override public void deleteBucket(AmazonS3 client, String bucketName) { if (client == null) { return; } else if (StringUtil.isEmpty(bucketName)) { return; } if (client.doesBucketExist(bucketName)) { client.deleteBucket(bucketName); } }
From source file:com.epam.dlab.module.aws.AdapterS3File.java
License:Apache License
/** * Creates and returns the Amazon client, as well as checks bucket existence. * * @throws AdapterException//from w ww. j a va 2 s .com */ private AmazonS3 getAmazonClient() throws AdapterException { AmazonS3 s3 = (accessKeyId == null ? new AmazonS3Client() : new AmazonS3Client(new BasicAWSCredentials(accessKeyId, secretAccessKey))); if (!s3.doesBucketExist(bucket)) { throw new AdapterException("Bucket \"" + bucket + "\" does not exist."); } return s3; }
From source file:com.kirana.services.ProductServicesImpl.java
@Override public boolean uploadProductImage(File productCsv, Shop shop, String productCode) throws Exception { AmazonS3 s3client = getS3Client(); if (!s3client.doesBucketExist(S3_BUCKET_NAME)) { s3client.createBucket(S3_BUCKET_NAME); }/*from ww w. j a v a 2 s.c om*/ s3client.putObject(S3_BUCKET_NAME, shop.getName() + "/" + productCode, productCsv); return true; }
From source file:com.maya.portAuthority.util.ImageUploader.java
public static void uploadImage(String imageURL, String imageName, String bucketName) throws MalformedURLException, IOException { // credentials object identifying user for authentication AWSCredentials credentials = new BasicAWSCredentials("AKIAJBFSMHRTIQQ7BKYA", "AdHgeP4dyWInWwPn9YlfxFCm3qP1lHjdxOxeJqDa"); // create a client connection based on credentials AmazonS3 s3client = new AmazonS3Client(credentials); String folderName = "image"; //folder name // String bucketName = "ppas-image-upload"; //must be unique try {//from ww w . ja v a2 s . co m if (!(s3client.doesBucketExist(bucketName))) { s3client.setRegion(Region.getRegion(Regions.US_EAST_1)); // Note that CreateBucketRequest does not specify region. So bucket is // created in the region specified in the client. s3client.createBucket(new CreateBucketRequest(bucketName)); } //Enabe CORS: // <?xml version="1.0" encoding="UTF-8"?> //<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> // <CORSRule> // <AllowedOrigin>http://ask-ifr-download.s3.amazonaws.com</AllowedOrigin> // <AllowedMethod>GET</AllowedMethod> // </CORSRule> //</CORSConfiguration> BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration(); CORSRule corsRule = new CORSRule() .withAllowedMethods( Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.GET })) .withAllowedOrigins(Arrays.asList(new String[] { "http://ask-ifr-download.s3.amazonaws.com" })); configuration.setRules(Arrays.asList(new CORSRule[] { corsRule })); s3client.setBucketCrossOriginConfiguration(bucketName, configuration); } 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()); } String fileName = folderName + SUFFIX + imageName + ".png"; URL url = new URL(imageURL); ObjectMetadata omd = new ObjectMetadata(); omd.setContentType("image/png"); omd.setContentLength(url.openConnection().getContentLength()); // upload file to folder and set it to public s3client.putObject(new PutObjectRequest(bucketName, fileName, url.openStream(), omd) .withCannedAcl(CannedAccessControlList.PublicRead)); }
From source file:com.openkm.util.backup.RepositoryS3Backup.java
License:Open Source License
/** * Performs a recursive repository content export with metadata *///from w ww. j av a2 s.co m public static ImpExpStats backup(String token, String fldPath, String bucket, boolean metadata, Writer out, InfoDecorator deco) throws PathNotFoundException, AccessDeniedException, RepositoryException, FileNotFoundException, ParseException, NoSuchGroupException, IOException, DatabaseException, GeneralException { log.debug("backup({}, {}, {}, {}, {}, {})", new Object[] { token, fldPath, bucket, metadata, out, deco }); ImpExpStats stats = null; if (running) { throw new GeneralException("Backup in progress"); } else { running = true; try { if (!Config.AMAZON_ACCESS_KEY.equals("") && !Config.AMAZON_SECRET_KEY.equals("")) { AmazonS3 s3 = new AmazonS3Client( new BasicAWSCredentials(Config.AMAZON_ACCESS_KEY, Config.AMAZON_SECRET_KEY)); if (!s3.doesBucketExist(bucket)) { s3.createBucket(bucket, Region.EU_Ireland); } stats = backupHelper(token, fldPath, s3, bucket, metadata, out, deco); log.info("Backup finished!"); } else { throw new GeneralException("Missing Amazon Web Service keys"); } } finally { running = false; } } log.debug("exportDocuments: {}", stats); return stats; }
From source file:datameer.awstasks.ant.s3.model.CreateBucketCommand.java
License:Apache License
@Override public void execute(Project project, AmazonS3 s3Service) { String name = getNormalizedName(); boolean doesBucketExist = s3Service.doesBucketExist(name); if (isEmptyIfExistent() && doesBucketExist) { List<S3ObjectSummary> s3Objects = s3Service.listObjects(name).getObjectSummaries(); for (S3ObjectSummary s3Object : s3Objects) { s3Service.deleteObject(name, s3Object.getKey()); }//from w ww. ja v a 2 s. co m doesBucketExist = false; } if (!doesBucketExist) { try { s3Service.createBucket(name, _location); System.out.println("created bucket '" + name + "'"); } catch (Exception e) { throw new RuntimeException("failed to create bucket '" + name + "'", e); } } }
From source file:doug.iotdemo.mojo.deployer.Deployer.java
License:Open Source License
@Override public void execute() throws MojoExecutionException, MojoFailureException { AmazonS3 s3 = new AmazonS3Client(); if (!s3.doesBucketExist(bucketName)) { throw new MojoExecutionException("bucket " + bucketName + " does not exist"); }/* w ww . ja v a 2 s .co m*/ getLog().info("Uploading " + source.getName() + " to s3://" + bucketName + "/" + bucketKey); TransferManager manager = new TransferManager(s3); Transfer transfer; if (source.isFile()) { transfer = manager.upload(bucketName, bucketKey, source); } else if (source.isDirectory()) { transfer = manager.uploadDirectory(bucketName, bucketKey, source, true); } else { throw new MojoExecutionException("Unknown file type " + source.getAbsolutePath()); } try { transfer.waitForCompletion(); } catch (InterruptedException e) { throw new MojoExecutionException("Upload to S3 failed", e); } }
From source file:ecplugins.s3.S3Util.java
License:Apache License
/** * This procedure deletes the bucket along with its contents * @param bucketName/*from ww w . j a va2s .c o m*/ * @return * @throws Exception */ public static boolean DeleteBucket(String bucketName) throws Exception { Properties props = TestUtils.getProperties(); BasicAWSCredentials credentials = new BasicAWSCredentials(props.getProperty(StringConstants.ACCESS_ID), props.getProperty(StringConstants.SECRET_ACCESS_ID)); // Create TransferManager TransferManager tx = new TransferManager(credentials); // Get S3 Client AmazonS3 s3 = tx.getAmazonS3Client(); if (s3.doesBucketExist(bucketName)) { // Multi-object delete by specifying only keys (no version ID). DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName).withQuiet(false); //get keys List<String> keys = new ArrayList<String>(); ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucketName)); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { keys.add(objectSummary.getKey()); } // Create request that include only object key names. List<DeleteObjectsRequest.KeyVersion> justKeys = new ArrayList<DeleteObjectsRequest.KeyVersion>(); for (String key : keys) { justKeys.add(new DeleteObjectsRequest.KeyVersion(key)); } if (justKeys.size() == 0) { return false; } multiObjectDeleteRequest.setKeys(justKeys); // Execute DeleteObjects - Amazon S3 add delete marker for each object // deletion. The objects no disappear from your bucket (verify). DeleteObjectsResult delObjRes = null; delObjRes = s3.deleteObjects(multiObjectDeleteRequest); s3.deleteBucket(bucketName); return true; } else { System.out.println("Error: Bucket with name " + bucketName + " does not exists."); return false; } }