public static void uploadToS3() { // upload to s3 bucket AWSCredentials awsCredentials = SkillConfig.getAWSCredentials(); AmazonS3Client s3Client = awsCredentials != null ? new AmazonS3Client(awsCredentials) : new AmazonS3Client(); File folder = new File("c:/temp/morse/" + DOT + "/mp3/"); File[] listOfFiles = folder.listFiles(); for (File file : listOfFiles) { if (file.isFile()) { if (!s3Client.doesObjectExist("morseskill", DOT + "/" + file.getName())) { PutObjectRequest s3Put = new PutObjectRequest("morseskill", DOT + "/" + file.getName(), file).withCannedAcl(CannedAccessControlList.PublicRead); s3Client.putObject(s3Put); System.out.println("Upload complete: " + file.getName()); } else { System.out.println("Skip as " + file.getName() + " already exists."); } } } }
@Test public void shutdownOfSqsAndS3FactoryCreatedClientsOccursWhenS3DeleteObjectFails() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); AmazonS3Client s3 = Mockito.mock(AmazonS3Client.class); String s3Id = "123"; SqsMessage m = new SqsMessage(RECEIPT_HANDLE, new byte[] {}, 1000, Optional.of(s3Id), new SqsMessage.Service(Optional.of(() -> s3), () -> sqs, Optional.of(s3), sqs, QUEUE, Optional.of(BUCKET))); Mockito.when(sqs.deleteMessage(QUEUE, RECEIPT_HANDLE)) .thenReturn(new DeleteMessageResult()); Mockito.doThrow(new RuntimeException()).when(s3).deleteObject(BUCKET, s3Id); try { m.deleteMessage(Client.FROM_FACTORY); Assert.fail(); } catch (RuntimeException e) { // do nothing } InOrder inorder = Mockito.inOrder(sqs, s3); inorder.verify(s3, Mockito.times(1)).deleteObject(BUCKET, s3Id); inorder.verify(s3, Mockito.times(1)).shutdown(); inorder.verify(sqs, Mockito.times(1)).shutdown(); Mockito.verifyNoMoreInteractions(sqs, s3); }
@Override public Seq<Binding<?>> bindings(Environment environment, Configuration configuration) { GlobalParams.AWS_ACCESS_KEY = Scala.orNull(configuration.getString("AWS_ACCESS_KEY", scala.Option.empty())); GlobalParams.AWS_SECRET_KEY = Scala.orNull(configuration.getString("AWS_SECRET_KEY", scala.Option.empty())); GlobalParams.AWS_S3_BUCKET = Scala.orNull(configuration.getString("AWS_S3_BUCKET", scala.Option.empty())); String accessKey = GlobalParams.AWS_ACCESS_KEY; String secretKey = GlobalParams.AWS_SECRET_KEY; s3Bucket = GlobalParams.AWS_S3_BUCKET; if ((accessKey != null) && (secretKey != null)) { awsCredentials = new BasicAWSCredentials(accessKey, secretKey); amazonS3 = new AmazonS3Client(awsCredentials); Logger.info("Using S3 Bucket: " + s3Bucket); } return seq( bind(S3Plugin.class).to(S3PluginImpl.class) ); }
/** * Test if custom endpoint is picked up. * <p/> * The test expects TEST_ENDPOINT to be defined in the Configuration * describing the endpoint of the bucket to which TEST_FS_S3A_NAME points * (f.i. "s3-eu-west-1.amazonaws.com" if the bucket is located in Ireland). * Evidently, the bucket has to be hosted in the region denoted by the * endpoint for the test to succeed. * <p/> * More info and the list of endpoint identifiers: * http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region * * @throws Exception */ @Test public void TestEndpoint() throws Exception { conf = new Configuration(); String endpoint = conf.getTrimmed(TEST_ENDPOINT, ""); if (endpoint.isEmpty()) { LOG.warn("Custom endpoint test skipped as " + TEST_ENDPOINT + "config " + "setting was not detected"); } else { conf.set(Constants.ENDPOINT, endpoint); fs = S3ATestUtils.createTestFileSystem(conf); AmazonS3Client s3 = fs.getAmazonS3Client(); String endPointRegion = ""; // Differentiate handling of "s3-" and "s3." based endpoint identifiers String[] endpointParts = StringUtils.split(endpoint, '.'); if (endpointParts.length == 3) { endPointRegion = endpointParts[0].substring(3); } else if (endpointParts.length == 4) { endPointRegion = endpointParts[1]; } else { fail("Unexpected endpoint"); } assertEquals("Endpoint config setting and bucket location differ: ", endPointRegion, s3.getBucketLocation(fs.getUri().getHost())); } }
public static String webHookDump(InputStream stream, String school, String extension) { if (stream != null) { extension = extension == null || extension.isEmpty() ? ".xml" : extension.contains(".") ? extension : "." + extension; String fileName = "webhooks/" + school + "/" + school + "_" + Clock.getCurrentDateDashes() + "_" + Clock.getCurrentTime() + extension; AmazonS3 s3 = new AmazonS3Client(); Region region = Region.getRegion(Regions.US_WEST_2); s3.setRegion(region); try { File file = CustomUtilities.inputStreamToFile(stream); s3.putObject(new PutObjectRequest(name, fileName, file)); return CustomUtilities.fileToString(file); } catch (Exception e) { e.printStackTrace(); } } return ""; }
@Override public AmazonS3Client load(S3ClientKey clientKey) throws Exception { logger.debug("Opening S3 client connection for {}", clientKey); ClientConfiguration clientConf = new ClientConfiguration(); clientConf.setProtocol(clientKey.isSecure ? Protocol.HTTPS : Protocol.HTTP); // Proxy settings (if configured) clientConf.setProxyHost(clientKey.s3Config.get(Constants.PROXY_HOST)); if (clientKey.s3Config.get(Constants.PROXY_PORT) != null) { clientConf.setProxyPort(Integer.valueOf(clientKey.s3Config.get(Constants.PROXY_PORT))); } clientConf.setProxyDomain(clientKey.s3Config.get(Constants.PROXY_DOMAIN)); clientConf.setProxyUsername(clientKey.s3Config.get(Constants.PROXY_USERNAME)); clientConf.setProxyPassword(clientKey.s3Config.get(Constants.PROXY_PASSWORD)); clientConf.setProxyWorkstation(clientKey.s3Config.get(Constants.PROXY_WORKSTATION)); if (clientKey.accessKey == null){ return new AmazonS3Client(new AnonymousAWSCredentialsProvider(), clientConf); } else { return new AmazonS3Client(new BasicAWSCredentials(clientKey.accessKey, clientKey.secretKey), clientConf); } }
/** * S3 储存客户端 * * @return 客户端 */ @Bean @ConditionalOnProperty(value = "bigbug.storage.s3.enable", havingValue = "true") AmazonS3Client amazonS3Client() { ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.HTTP); BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials( storageProperties.getStorage().getS3().getAccessKey(), storageProperties.getStorage().getS3().getSecretKey()); return (AmazonS3Client) AmazonS3ClientBuilder.standard() .withClientConfiguration(clientConfig) .withEndpointConfiguration( new AwsClientBuilder.EndpointConfiguration( storageProperties.getStorage().getS3().getEndpoint(), Regions.DEFAULT_REGION.getName())) .withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)) .build(); }
/** * Creates a new S3 configuration cache. * @param bucket The S3 bucket. * @param prefix The S3 object prefix. * @param pathPattern The path pattern. * @param accessKey The (optional) S3 access key. * @param secretKey The (optional) S3 secret key. * @param region The AWS region (e.g. us-east-1). * @throws IOException Thrown if the configuration cannot be read. */ public S3ConfigurationCache(String bucket, String prefix, String pathPattern, String accessKey, String secretKey, String region) throws IOException { this.bucket = bucket; this.prefix = prefix; this.pathPattern = pathPattern; if (!StringUtils.isEmpty(accessKey)) { s3 = AmazonS3Client.builder() .withCredentials(new AWSStaticCredentialsProvider( new BasicAWSCredentials(accessKey, secretKey))) .withRegion(Regions.fromName(region)) .build(); } else { s3 = AmazonS3Client.builder() .withRegion(Regions.fromName(region)) .build(); } }
public S3StorageService() { CloudStorageSettings settings = Settings.instance().getCloudStorage(); if (settings == null) { throw new ConfigException("You are missing the section [cloudStorage]\naccessToken=...\nsecret=... in your stallion.toml"); } if (empty(settings.getAccessToken())) { throw new ConfigException("You are missing the setting accessKey in the stallion.toml section [cloudStorage]"); } if (empty(settings.getSecret())) { throw new ConfigException("You are missing the setting secret in the stallion.toml section [cloudStorage]"); } accessToken = settings.getAccessToken(); secret = settings.getSecret(); AWSCredentials credentials = new BasicAWSCredentials(accessToken, secret); client = new AmazonS3Client(credentials); }
@Override public PutObjectResult putObject(PutObjectRequest request) throws Exception { RefCountedClient holder = client.get(); AmazonS3Client amazonS3Client = holder.useClient(); try { return amazonS3Client.putObject(request); } finally { holder.release(); } }
@Override public void remove(String accessKey) throws Exception { LogUtils.debug(LOG_TAG, "Deleting file with access key: " + accessKey); AmazonS3 s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain()); DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName); List<KeyVersion> keys = new ArrayList<KeyVersion>(); keys.add(new KeyVersion(accessKey)); keys.add(new KeyVersion(accessKey + "_key")); multiObjectDeleteRequest.setKeys(keys); s3Client.deleteObjects(multiObjectDeleteRequest); LogUtils.debug(LOG_TAG, "Deleted file with access key: " + accessKey); }
@Override public void init(Configuration conf, String keyPrefix) { bucketName = conf.get(keyPrefix + S3_BUCKET_NAME); String endpoint = conf.get(keyPrefix + S3_ENDPOINT_NAME); String key = conf.get(keyPrefix + S3_ACCESS_KEY); String secret = conf.get(keyPrefix + S3_ACCESS_SECRET); System.setProperty(SDKGlobalConfiguration.ACCESS_KEY_SYSTEM_PROPERTY, key); System.setProperty(SDKGlobalConfiguration.SECRET_KEY_SYSTEM_PROPERTY, secret); AWSCredentialsProvider provider = new SystemPropertiesCredentialsProvider(); client = new AmazonS3Client(provider); client.setEndpoint(endpoint); override = conf.getBoolean(keyPrefix + "override", true); acls = new AccessControlList(); acls.grantPermission(GroupGrantee.AllUsers, Permission.FullControl); acls.grantPermission(GroupGrantee.AllUsers, Permission.Read); acls.grantPermission(GroupGrantee.AllUsers, Permission.Write); }
@Override @SuppressWarnings("unused") public List<AwsFileMiniModel> list(String prefix) { AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); List<AwsFileMiniModel> files = new ArrayList(); ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(prefix); ObjectListing objectListing; do { objectListing = s3client.listObjects(listObjectsRequest); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println(" - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")" + " (date = " + objectSummary.getLastModified() + ")"); files.add(new AwsFileMiniModel(objectSummary.getKey(), objectSummary.getLastModified())); } listObjectsRequest.setMarker(objectListing.getNextMarker()); } while (objectListing.isTruncated()); return files; }
@Override public void start(Map<String, String> properties) { configProperties = properties; config = new RedshiftSinkTaskConfig(configProperties); tempDir = new File(config.getTempOutputDir()); tempDir.mkdirs(); tempFiles = new HashMap<>(); writers = new HashMap<>(); List<String> fields = config.getFields(); if (fields.size() == 1 && fields.get(0).equals("*")) { fields.clear(); } serializer = new DefaultCopySerializer(fields); s3 = new AmazonS3Client(config.getAwsCredentials()); }
private AmazonS3Client givenS3Mock() { return Mockito.mock(AmazonS3Client.class, new Answer() { @Override public Object answer(final InvocationOnMock invocation) throws Throwable { if (invocation.getMethod().getName().equals("getObject")) { // second parameter is the path to the file final String resourcePath = invocation.getArguments()[1].toString(); // extract leading path final String leadingPath = resourcePath.substring(0, resourcePath.indexOf(LOCALE)); // extract trailing path final String trailingPath = resourcePath.substring(resourcePath.indexOf(LOCALE) + LOCALE.length() + 1); // we reroute this request to the resourceutterancereader final ResourceUtteranceReader resourceReader = new ResourceUtteranceReader(leadingPath); resourceReader.setResourceLocation(trailingPath); final S3Object s3Object = new S3Object(); s3Object.setObjectContent(resourceReader.read(LOCALE)); return s3Object; } return invocation.callRealMethod(); } }); }
@Test public void testConstructors() throws Exception { final S3UtteranceReader reader = new S3UtteranceReader("bucketName"); Assert.assertEquals("bucketName", reader.getBucketName()); final S3UtteranceReader reader2 = new S3UtteranceReader("bucketName", "/leading/path/"); Assert.assertEquals("bucketName", reader2.getBucketName()); Assert.assertEquals("leading/path/", reader2.getLeadingPath()); final S3UtteranceReader reader3 = new S3UtteranceReader("bucketName", "leading/path", "trailing/test.yml"); Assert.assertEquals("bucketName", reader3.getBucketName()); Assert.assertEquals("leading/path/", reader3.getLeadingPath()); Assert.assertEquals("/trailing/test.yml", reader3.getResourceLocation()); exception.expect(IllegalArgumentException.class); new S3UtteranceReader(null, "bucketName", "leadingPath"); exception.expect(IllegalArgumentException.class); new S3UtteranceReader(""); exception.expect(IllegalArgumentException.class); new S3UtteranceReader("", "leadingPath"); exception.expect(IllegalArgumentException.class); new S3UtteranceReader(new AmazonS3Client(), "", "leadingPath", S3UtteranceReader.DEFAULT_RESOURCE_LOCATION); }
public AmazonS3 configureS3(){ /* * Create your credentials file at ~/.aws/credentials (C:\Users\USER_NAME\.aws\credentials for Windows users) * and save the following lines after replacing the underlined values with your own. * * [default] * aws_access_key_id = YOUR_ACCESS_KEY_ID * aws_secret_access_key = YOUR_SECRET_ACCESS_KEY */ AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider().getCredentials(); } catch (Exception e) { throw new AmazonClientException( "Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (~/.aws/credentials), and is in valid format.", e); } AmazonS3 s3 = new AmazonS3Client(credentials); Region usEast1 = Region.getRegion(Regions.US_EAST_1); s3.setRegion(usEast1); return s3; }
@Override public void setConf(AbstractConfig config) { this.config = (GeoIpOperationConfig) config; AmazonS3Client client = this.s3Factory.newInstance(); AmazonS3URI uri = new AmazonS3URI(this.config.getGeoLiteDb()); GetObjectRequest req = new GetObjectRequest(uri.getBucket(), uri.getKey()); S3Object obj = client.getObject(req); try { this.databaseReader = new DatabaseReader.Builder(obj.getObjectContent()).withCache(new CHMCache()).build(); } catch (IOException e) { throw new ConfigurationException("Unable to read " + this.config.getGeoLiteDb(), e); } }
@Override public void start(Map<String, String> props) throws ConnectException { readConfig(props); // Use default credentials provider that looks in Env + Java properties + profile + instance role AmazonS3 s3Client = new AmazonS3Client(); // If worker config sets explicit endpoint override (e.g. for testing) use that if (overrideS3Endpoint != "") { s3Client.setEndpoint(overrideS3Endpoint); } if (s3PathStyle) { s3Client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true)); } s3 = new S3Writer(bucket, prefix, s3Client); // Recover initial assignments Set<TopicPartition> assignment = context.assignment(); recoverAssignment(assignment); }
private S3TransferHelper(final Context context, final AmazonS3Client s3Client, final String bucket, final String s3DirPrefix, final String localTransferPath, final LocalContentCache cache) { this.bucket = bucket; this.s3DirPrefix = s3DirPrefix == null ? "" : s3DirPrefix; this.localContentCache = cache; if (localTransferPath.endsWith(DIR_DELIMITER)) { this.localTransferPath = localTransferPath; } else { this.localTransferPath = localTransferPath + DIR_DELIMITER; } transferUtility = new TransferUtility(s3Client, context); transfersInProgress = new HashMap<>(); managedFilesToTransfers = new HashMap<>(); progressListeners = new HashMap<>(); }
@SuppressWarnings("unchecked") @Test public void ensureIfSendToSqsFailsThatS3ObjectIsDeleted() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); AmazonS3Client s3 = Mockito.mock(AmazonS3Client.class); Mockito.when(sqs.sendMessage(Mockito.anyString(), Mockito.anyString())).thenThrow(RuntimeException.class); try { Sqs.sendToQueueUsingS3(sqs, "queueUrl", s3, "bucket", new byte[] { 1, 2 }); } catch (RuntimeException e) { assertTrue(e instanceof CompositeException); InOrder inorder = Mockito.inOrder(sqs, s3); inorder.verify(s3, Mockito.times(1)).putObject(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any()); inorder.verify(sqs, Mockito.times(1)).sendMessage(Mockito.anyString(), Mockito.anyString()); inorder.verify(s3, Mockito.times(1)).deleteObject(Mockito.anyString(), Mockito.anyString()); inorder.verifyNoMoreInteractions(); } }
public static void deleteConfig(String id) throws LoadConfigException { if (configs.remove(id) == null) { throw new LoadConfigException("Could not find config " + id); } String root = SystemConfig.getLoadConfigRoot(); if (isLocalFilesystem(root)) { File f = new File(root, id + ".json"); if (!f.exists()) { LOG.log(Level.INFO, "Config {0} does not exist in storage {1} so doing nothing which is idempotent", new Object[]{id, root}); } f.delete(); } else if (isS3(root)) { AmazonS3 api = new AmazonS3Client(SystemConfig.getCredentials()); SystemConfig.S3Info info = SystemConfig.getS3Info(SystemConfig.getConfigRoot()); byte[] data; String key = info.prefixPath + "/" + id + ".json"; api.deleteObject(info.bucket, key); LOG.log(Level.INFO, "successfully deleted MetricsConfiguration in S3 {0}/{1}", new Object[]{info.bucket, key}); } }
public static AmazonS3Client getS3Client(AuthCredentialsServiceState credentials, String regionId) { ClientConfiguration configuration = new ClientConfiguration(); configuration.withRetryPolicy(new RetryPolicy(new CustomRetryCondition(), DEFAULT_BACKOFF_STRATEGY, DEFAULT_MAX_ERROR_RETRY, false)); AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider( new BasicAWSCredentials(credentials.privateKeyId, EncryptionUtils.decrypt(credentials.privateKey))); AmazonS3ClientBuilder amazonS3ClientBuilder = AmazonS3ClientBuilder .standard() .withClientConfiguration(configuration) .withCredentials(awsStaticCredentialsProvider) .withRegion(regionId); if (isAwsClientMock()) { throw new IllegalArgumentException("AWS Mock does not support S3 client"); } return (AmazonS3Client) amazonS3ClientBuilder.build(); }
private Optional<String> findBucket(final String environmentName) { AmazonS3Client s3Client = new AmazonS3Client(); List<Bucket> buckets = s3Client.listBuckets(); String envBucket = null; for (final Bucket bucket : buckets) { if (StringUtils.contains(bucket.getName(), ConfigConstants.CONFIG_BUCKET_KEY)) { String[] parts = bucket.getName().split("-"); if (StringUtils.equalsIgnoreCase(environmentName, parts[0])) { envBucket = bucket.getName(); break; } } } return Optional.ofNullable(envBucket); }
private static Observable<SqsMessage> get(AmazonSQSClient sqs, String queueName, Optional<String> bucketName, Optional<AmazonS3Client> s3, Service service, int waitTimeSeconds) { return Observable.defer(() -> { String queueUrl = sqs.getQueueUrl(queueName).getQueueUrl(); return Observable .just(sqs.receiveMessage(request(queueName, waitTimeSeconds)) // .getMessages() // .stream() // .map(m -> Sqs.getNextMessage(m, queueUrl, bucketName, s3, sqs, service)) // .collect(Collectors.toList())) // .concatWith(Observable .defer(() -> Observable.just(sqs.receiveMessage(request(queueName, 0)) // .getMessages() // .stream() // .map(m -> Sqs.getNextMessage(m, queueUrl, bucketName, s3, sqs, service)) // .collect(Collectors.toList()))) // .repeat()) .takeWhile(list -> !list.isEmpty()) // .flatMapIterable(Functions.identity()) // .filter(opt -> opt.isPresent()).map(opt -> opt.get()); });// }
public void setup() { AWSCredentialsProvider awsCredentialsProvider = new DefaultAWSCredentialsProviderChain(); try { awsCredentialsProvider.getCredentials(); s3Bucket = System.getenv("CLOUDKEEPER_S3_TEST_BUCKET"); if (s3Bucket == null) { s3Bucket = System.getProperty("xyz.cloudkeeper.s3.testbucket"); } if (s3Bucket != null) { executorService = Executors.newScheduledThreadPool(4); AmazonS3 s3Client = new AmazonS3Client(awsCredentialsProvider); s3Connection = new S3ConnectionBuilder(s3Client, executorService).build(); skipTest = false; cleanS3(s3Connection, s3Bucket); } } catch (AmazonClientException exception) { credentialsException = exception; } }
@Test public void deleteMessageFromFactoryWhenS3FactoryExists() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); AmazonS3Client s3 = Mockito.mock(AmazonS3Client.class); String s3Id = "123"; SqsMessage m = new SqsMessage(RECEIPT_HANDLE, new byte[] {}, 1000, Optional.of(s3Id), new SqsMessage.Service(Optional.of(() -> s3), () -> sqs, Optional.of(s3), sqs, QUEUE, Optional.of(BUCKET))); Mockito.when(sqs.deleteMessage(QUEUE, RECEIPT_HANDLE)) .thenReturn(new DeleteMessageResult()); m.deleteMessage(Client.FROM_FACTORY); InOrder inorder = Mockito.inOrder(sqs, s3); inorder.verify(s3, Mockito.times(1)).deleteObject(BUCKET, s3Id); inorder.verify(sqs, Mockito.times(1)).deleteMessage(QUEUE, RECEIPT_HANDLE); inorder.verify(s3, Mockito.times(1)).shutdown(); inorder.verify(sqs, Mockito.times(1)).shutdown(); Mockito.verifyNoMoreInteractions(sqs, s3); }
protected static String uploadFileToS3(BufferedImage image, String word, Boolean codeOnly) throws IOException { ByteArrayInputStream bis = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { String bucket = SkillConfig.getS3BucketName(); String fileKey = getFileKey(word, codeOnly); ImageIO.write(image, "png", bos); byte[] bImageData = bos.toByteArray(); bis = new ByteArrayInputStream(bImageData); // upload to s3 bucket AWSCredentials awsCredentials = SkillConfig.getAWSCredentials(); AmazonS3Client s3Client = awsCredentials != null ? new AmazonS3Client(awsCredentials) : new AmazonS3Client(); PutObjectRequest s3Put = new PutObjectRequest(bucket, fileKey, bis, null).withCannedAcl(CannedAccessControlList.PublicRead); s3Client.putObject(s3Put); return getS3Url(word, codeOnly); } finally { bos.close(); if (bis != null) bis.close(); } }
String getPrefixKey(BackupRestoreContext ctx) throws URISyntaxException { URI uri = new URI(ctx.getExternalLocation()); String[] segments = uri.getPath().split("/"); int startIndex = uri.getScheme().equals(AmazonS3Client.S3_SERVICE_NAME) ? 1 : 2; String prefixKey = ""; for (int i=startIndex; i<segments.length; i++) { prefixKey += segments[i]; if (i < segments.length - 1) { prefixKey += "/"; } } prefixKey = (prefixKey.length() > 0 && !prefixKey.endsWith("/")) ? prefixKey + "/" : prefixKey; prefixKey += ctx.getName(); // append backup name return prefixKey; }
String getEndpoint(BackupRestoreContext ctx) throws URISyntaxException { URI uri = new URI(ctx.getExternalLocation()); String scheme = uri.getScheme(); if (scheme.equals(AmazonS3Client.S3_SERVICE_NAME)) { return Constants.S3_HOSTNAME; } else { String endpoint = scheme + "://" + uri.getHost(); int port = uri.getPort(); if (port != -1) { endpoint += ":" + Integer.toString(port); } return endpoint; } }
private static Map<String, Long> listSnapshotFiles(AmazonS3Client amazonS3Client, String bucketName, String backupName) { Map<String, Long> snapshotFiles = new HashMap<>(); final ListObjectsV2Request req = new ListObjectsV2Request() .withBucketName(bucketName) .withPrefix(backupName); ListObjectsV2Result result; do { result = amazonS3Client.listObjectsV2(req); for (S3ObjectSummary objectSummary : result.getObjectSummaries()) { snapshotFiles.put ( objectSummary.getKey ( ), objectSummary.getSize()); } req.setContinuationToken(result.getNextContinuationToken()); } while(result.isTruncated()); return snapshotFiles; }
/** * Constructor without privided credentials to deleguate to the default provider chain. * @since 3.1 */ @Incubating public S3Client(S3ConnectionProperties s3ConnectionProperties) { this.s3ConnectionProperties = s3ConnectionProperties; amazonS3Client = new AmazonS3Client(createConnectionProperties()); setAmazonS3ConnectionEndpoint(); }
NetworkStore() { kwalaCookieStore = new KwalaCookieStore(KwalaApplication.getInstance()); CookieManager cookieManager = new CookieManager(kwalaCookieStore, CookiePolicy.ACCEPT_ORIGINAL_SERVER); CookieHandler.setDefault(cookieManager); okHttpClient = new OkHttpClient.Builder() .cookieJar(new JavaNetCookieJar(cookieManager)) .authenticator(authenticator) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) .build(); KwalaApplication application = KwalaApplication.getInstance(); // Initialize the Amazon Cognito credentials provider CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(application, KwalaConstants.Network.AWS_IDENTITY_POOL_ID, Regions.US_EAST_1 ); // Initialize Amazon S3 and transfer utility s3Client = new AmazonS3Client(credentialsProvider); transferUtility = new TransferUtility(s3Client, application); }
public void remove(Object[] params) { AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); try { s3client.deleteObject(new DeleteObjectRequest(bucketName, params[0].toString())); } 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()); } }
public Object get(Object[] params) { AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); try { S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, params[0].toString())); InputStream objectData = object.getObjectContent(); byte[] bytes = IOUtils.toByteArray(is); ByteBuffer b = ByteBuffer.wrap(bytes); return b; } 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()); } }
public void put(Object[] params) { String key = params[0].toString(); File file = new File(params[0].toString()); AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); try { s3client.putObject(new PutObjectRequest(bucketName, key, file)); } 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()); } }