/** * Returns a new client with region configured to * region. * Also updates the clientsByRegion map by associating the * new client with region. * * @param region * The region the returned {@link AmazonS3} will be * configured to use. * @return A new {@link AmazonS3} client with region set to region. */ private AmazonS3 cacheClient(String region) { if (awscredentialsProvider == null) { throw new IllegalArgumentException("No credentials provider found to connect to S3"); } AmazonS3 client = new AmazonS3Client(awscredentialsProvider); client.setRegion(RegionUtils.getRegion(region)); clientsByRegion.put(region, client); return client; }
s3.setRegion(awsRegion);
private AmazonS3 getS3Client() throws Exception{ this.bucketName = properties.getProperty( "usergrid.binary.bucketname" ); if(bucketName == null){ logger.error( "usergrid.binary.bucketname not properly set so amazon bucket is null" ); throw new AwsPropertiesNotFoundException( "usergrid.binary.bucketname" ); } final UsergridAwsCredentialsProvider ugProvider = new UsergridAwsCredentialsProvider(); AWSCredentials credentials = ugProvider.getCredentials(); ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.HTTP); s3Client = new AmazonS3Client(credentials, clientConfig); if(regionName != null) s3Client.setRegion( Region.getRegion(Regions.fromName(regionName)) ); return s3Client; }
client.setEndpoint(endpoint); } else if (!region.isEmpty()) { client.setRegion(Region.getRegion(Regions.fromName(region)));
/** * * @return TransferUtility */ public TransferUtility build() { if (this.s3 == null) { throw new IllegalArgumentException( "AmazonS3 client is required please set using .s3Client(yourClient)"); } else if (this.appContext == null) { throw new IllegalArgumentException("Context is required please set using .context(applicationContext)"); } if (this.awsConfig != null) { try { final JSONObject tuConfig = this.awsConfig.optJsonObject("S3TransferUtility"); this.s3.setRegion(Region.getRegion(tuConfig.getString("Region"))); this.defaultBucket = tuConfig.getString("Bucket"); TransferUtility.setUserAgentFromConfig(this.awsConfig.getUserAgent()); } catch (Exception e) { throw new IllegalArgumentException("Failed to read S3TransferUtility " + "please check your setup or awsconfiguration.json file", e); } } if (this.transferUtilityOptions == null) { this.transferUtilityOptions = new TransferUtilityOptions(); } return new TransferUtility(this.s3, this.appContext, this.defaultBucket, this.transferUtilityOptions); } }
@Override public void setRegion(Region region) throws IllegalArgumentException { getDelegate().setRegion(region); }
AmazonS3 s3 = new AmazonS3Client(); s3.setRegion(Region.getRegion(Regions.US_WEST_2)); // or whatever region you are working with
public AmazonS3 getS3Client(final AWSCredentialsProvider credentialsProvider) { Objects.requireNonNull(credentialsProvider, "credentialsProvider must not be null"); Objects.requireNonNull(region, "region must not be null"); final AmazonS3 client = s3ClientFactory.getS3Client(credentialsProvider, new ClientConfiguration(clientCfg).withSignerOverride("AWSS3V4SignerType")); client.setRegion(region); return client; }
@Test public void testNoMetaData() { AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); AmazonS3 amazonS3 = new AmazonS3Client(awsCredentials); amazonS3.setRegion(Region.getRegion(region)); byte[] bytes = content.getBytes(StandardCharsets.UTF_8); ObjectMetadata metadata = new ObjectMetadata(); metadata.setSSEAlgorithm(SSEAlgorithm.KMS.getAlgorithm()); InputStream inputStream = new ByteArrayInputStream(bytes); PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, metadata); putObjectRequest.withSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(awsKmsKeyId)); amazonS3.putObject(putObjectRequest); }
/** * Returns a new client with region configured to * region. * Also updates the clientsByRegion map by associating the * new client with region. * * @param region * The region the returned {@link AmazonS3} will be * configured to use. * @return A new {@link AmazonS3} client with region set to region. */ private AmazonS3 cacheClient(String region) { if (awscredentialsProvider == null) { throw new IllegalArgumentException("No credentials provider found to connect to S3"); } AmazonS3 client = new AmazonS3Client(awscredentialsProvider); client.setRegion(RegionUtils.getRegion(region)); clientsByRegion.put(region, client); return client; }
private static final int BYTES_DOWNLOAD = 1024; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String key = request.getParameter("key"); HttpSession session = request.getSession(); String bucketname = (String) session.getAttribute("BUCKETNAME"); AmazonS3 s3 = new AmazonS3Client(Constants.credentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); S3Object object = s3.getObject(new GetObjectRequest(bucketname, key)); response.setContentType(object.getObjectMetadata().getContentType()); response.setHeader("Content-Disposition","attachment;filename="+object.getKey()); InputStream is = object.getObjectContent();int read=0; byte[] bytes = new byte[BYTES_DOWNLOAD]; OutputStream os = response.getOutputStream(); while((read = is.read(bytes))!= -1){ os.write(bytes, 0, read); } os.flush(); os.close(); }
@Override public void setup(Context.OperatorContext context) { s3client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey)); if (region != null) { s3client.setRegion(Region.getRegion(Regions.fromName(region))); } filePath = context.getValue(DAG.APPLICATION_PATH); try { fs = FileSystem.newInstance(new Path(filePath).toUri(), new Configuration()); } catch (IOException e) { logger.error("Unable to create FileSystem: {}", e.getMessage()); } super.setup(context); }
private static boolean doPutObject(Region region, String bucket, String key, InputStream is, ObjectMetadata metaData) { try { String regionName = ""; if (region.toString() != null) { regionName = region.toString(); } else { regionName = "us-east-1"; } logger.debug("Setregion: {}", regionName); // need to set the region for "eu-central-1" region to work // this enables V4 signing // careful, this is not thread-safe! s3client.setRegion(RegionUtils.getRegion(regionName)); logger.debug("PUT object to S3 bucket: {}", bucket); s3client.putObject(bucket, key, is, metaData); return true; } catch (Exception e) { logger.error("Error putting object", e); return false; } }
/** * * @return TransferUtility */ public TransferUtility build() { if (this.s3 == null) { throw new IllegalArgumentException( "AmazonS3 client is required please set using .s3Client(yourClient)"); } else if (this.appContext == null) { throw new IllegalArgumentException("Context is required please set using .context(applicationContext)"); } if (this.awsConfig != null) { try { final JSONObject tuConfig = this.awsConfig.optJsonObject("S3TransferUtility"); this.s3.setRegion(Region.getRegion(tuConfig.getString("Region"))); this.defaultBucket = tuConfig.getString("Bucket"); TransferUtility.setUserAgentFromConfig(this.awsConfig.getUserAgent()); } catch (Exception e) { throw new IllegalArgumentException("Failed to read S3TransferUtility " + "please check your setup or awsconfiguration.json file", e); } } if (this.transferUtilityOptions == null) { this.transferUtilityOptions = new TransferUtilityOptions(); } return new TransferUtility(this.s3, this.appContext, this.defaultBucket, this.transferUtilityOptions); } }
private static void doDeleteObject(Region region, String bucket, String key) { try { String regionName = ""; if (region.toString() != null) { regionName = region.toString(); } else { regionName = "us-east-1"; } logger.debug("Setregion: {}", regionName); // need to set the region for "eu-central-1" region to work // this enables V4 signing // careful, this is not thread-safe! s3client.setRegion(RegionUtils.getRegion(regionName)); logger.debug("DELETE object from S3 bucket: {}", bucket); s3client.deleteObject(bucket, key); } catch (Exception e) { logger.error("Error deleting object", e); return; } } }
private static AmazonS3 getS3Client() { Regions region; if (System.getProperty("AWS_REGION") != null) { try { region = Regions.valueOf(System.getProperty("AWS_REGION")); } catch (IllegalArgumentException e) { // probably not great to have a default, but we can't just blow up if this // property isn't set LOGGER.warning( "AWS_REGION property is set, but not set correctly. " + "Check that the AWS_REGION property matches the Regions enum"); region = Regions.US_EAST_1; } } else { LOGGER.warning("No AWS_REGION property set, defaulting to US_EAST_1"); region = Regions.US_EAST_1; } AmazonS3 s3; if (Boolean.getBoolean("S3_USE_ANON")) { s3 = new AmazonS3Client(new AnonymousAWSCredentials()); } else { s3 = new AmazonS3Client(); } s3.setRegion(Region.getRegion(region)); return s3; }
byte[] bI = org.apache.commons.codec.binary.Base64.decodeBase64((base64Data.substring(base64Data.indexOf(",")+1)).getBytes()); InputStream fis = new ByteArrayInputStream(bI); AmazonS3 s3 = new AmazonS3Client(); Region usWest02 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest02); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(bI.length); metadata.setContentType("image/png"); metadata.setCacheControl("public, max-age=31536000"); s3.putObject(BUCKET_NAME, filename, fis, metadata); s3.setObjectAcl(BUCKET_NAME, filename, CannedAccessControlList.PublicRead);
private void detectEndpointFromBucket() { String location = this.amazonS3.getBucketLocation(this.bucketName); try { Region region = Region.fromLocationConstraint(this.amazonS3.getBucketLocation(this.bucketName)); this.amazonS3.setEndpoint(region.getEndpoint()); } catch (IllegalArgumentException e) { this.amazonS3.setRegion(parseRegion(location)); } }
public AmazonS3 getS3client() { BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, securityKey); if (testing && (!testingUrl.isEmpty() || !testingUrl.equals(""))) { s3client = new AmazonS3Client(awsCreds); s3client.setRegion(Region.getRegion(Regions.fromName(bucketRegion))); s3client.setEndpoint(testingUrl); s3client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).disableChunkedEncoding().build()); } else { s3client = AmazonS3ClientBuilder.standard().withRegion(Regions.fromName(bucketRegion)) .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build(); } return s3client; }
s3 = new AmazonS3Client(credentials, s3ClientConfig); Region euCentral = Region.getRegion(Regions.EU_CENTRAL_1); s3.setRegion(euCentral);