@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); }
@Test public void testPut() { ModelBucket bucket = getService(ModelBucket.class); InputStream stream = new ByteArrayInputStream("file content".getBytes()); ArgumentCaptor<PutObjectRequest> requestCaptor = ArgumentCaptor.forClass(PutObjectRequest.class); PutObjectResult expected = new PutObjectResult(); when(amazonS3Client.putObject(requestCaptor.capture())).thenReturn(expected); assertEquals(expected, bucket.put("path", stream, 12L)); PutObjectRequest request = requestCaptor.getValue(); assertEquals("model-bucket", request.getBucketName()); assertEquals("path", request.getKey()); assertEquals(stream, request.getInputStream()); assertEquals(12L, request.getMetadata().getContentLength()); List<Grant> grants = request.getAccessControlList().getGrantsAsList(); assertEquals(1, grants.size()); assertEquals(GroupGrantee.AllUsers, grants.get(0).getGrantee()); assertEquals(Permission.Read, grants.get(0).getPermission()); }
static public void uploadToAmazonS3(HttpSession session, File fileToUpload) throws S3Exception { try { AmazonS3 s3client = getS3(); String bucketName = getDownloadS3Bucket(); if(!s3client.doesBucketExist(bucketName)) SagLogger.logError(session, "Does not exist? S3 Bucket :" + bucketName); AccessControlList acl = new AccessControlList(); acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); s3client.putObject(new PutObjectRequest(bucketName, getAPKDownloadFilePathWithFile(fileToUpload.getName()), fileToUpload).withAccessControlList(acl)); SagLogger.logInfo(session, "Finished uploading to S3"); } catch (Exception e) { SagLogger.logException(session, e); throw new S3Exception(e); } }
@Test public void testUpdateBlobXmlAcls() throws Exception { assumeTrue(!Quirks.NO_BLOB_ACCESS_CONTROL.contains(blobStoreType)); String blobName = "testUpdateBlobXmlAcls-blob"; ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(BYTE_SOURCE.size()); client.putObject(containerName, blobName, BYTE_SOURCE.openStream(), metadata); AccessControlList acl = client.getObjectAcl(containerName, blobName); acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); client.setObjectAcl(containerName, blobName, acl); assertThat(client.getObjectAcl(containerName, blobName)).isEqualTo(acl); acl.revokeAllPermissions(GroupGrantee.AllUsers); client.setObjectAcl(containerName, blobName, acl); assertThat(client.getObjectAcl(containerName, blobName)).isEqualTo(acl); acl.grantPermission(GroupGrantee.AllUsers, Permission.Write); try { client.setObjectAcl(containerName, blobName, acl); Fail.failBecauseExceptionWasNotThrown(AmazonS3Exception.class); } catch (AmazonS3Exception e) { assertThat(e.getErrorCode()).isEqualTo("NotImplemented"); } }
public Object put(String path, InputStream fileInputStream, Long contentLenght) { if (contentLenght == null || contentLenght <= 0) { FindContentLengthResult result = findContentLength(fileInputStream); contentLenght = result.getContentLength(); fileInputStream = result.getFileInputStream(); } ObjectMetadata meta = new ObjectMetadata(); meta.setContentLength(contentLenght); AccessControlList acl = new AccessControlList(); acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); return amazonS3Client.putObject(new PutObjectRequest(getBucketName(), path, fileInputStream, meta).withAccessControlList(acl)); }
@Test public void testPutWithoutContentLenght() { ModelBucket bucket = getService(ModelBucket.class); InputStream stream = new ByteArrayInputStream("file content".getBytes()); ArgumentCaptor<PutObjectRequest> requestCaptor = ArgumentCaptor.forClass(PutObjectRequest.class); PutObjectResult expected = new PutObjectResult(); when(amazonS3Client.putObject(requestCaptor.capture())).thenReturn(expected); assertEquals(expected, bucket.put("path", stream, null)); PutObjectRequest request = requestCaptor.getValue(); Scanner scanner = new Scanner(request.getInputStream()); assertEquals("model-bucket", request.getBucketName()); assertEquals("path", request.getKey()); assertEquals("file content", scanner.useDelimiter("\\A").next()); assertEquals(12L, request.getMetadata().getContentLength()); List<Grant> grants = request.getAccessControlList().getGrantsAsList(); assertEquals(1, grants.size()); assertEquals(GroupGrantee.AllUsers, grants.get(0).getGrantee()); assertEquals(Permission.Read, grants.get(0).getPermission()); scanner.close(); }
@Override public AccessControlList getBucketAcl(String bucketName) throws AmazonClientException, AmazonServiceException { throwException(getBucketAclException); AccessControlList acl = new AccessControlList(); acl.grantPermission(GroupGrantee.AllUsers, Permission.FullControl); return acl; }
public void deploy(AwsKeyPair keyPair, String region, String inputDirectory, final String bucketName, final String outputBasePath, Proxy proxy) { final AWSCredentialsProvider credentials = new AWSStaticCredentialsProvider( new BasicAWSCredentials(keyPair.key, keyPair.secret)); ClientConfiguration cc = Util.createConfiguration(proxy); final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withCredentials(credentials).withClientConfiguration(cc) .withRegion(region).build(); if (inputDirectory == null) { throw new RuntimeException("must specify inputDirectory parameter in configuration"); } final Path root = new File(inputDirectory).toPath().toAbsolutePath(); try { Files.walkFileTree(root, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { AccessControlList acl = new AccessControlList(); acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); String relativePath = root.relativize(file.toAbsolutePath()).toString(); String objectName; if (outputBasePath != null) { objectName = outputBasePath + "/" + relativePath; } else { objectName = relativePath; } log.info("uploading " + file.toFile() + " to " + bucketName + ":" + objectName); PutObjectRequest req = new PutObjectRequest(bucketName, objectName, file.toFile()) // .withAccessControlList(acl); s3.putObject(req); return FileVisitResult.CONTINUE; } }); log.info("uploaded files"); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Returns an XML fragment representing the specified Grantee. * * @param grantee * The grantee to convert to an XML representation that can be * sent to Amazon S3 as part of a request. * @param xml * The XmlWriter to which to concatenate this node to. * * @return The given XmlWriter containing the specified grantee. * * @throws SdkClientException * If the specified grantee type isn't recognized. */ protected XmlWriter convertToXml(Grantee grantee, XmlWriter xml) throws SdkClientException { if (grantee instanceof CanonicalGrantee) { return convertToXml((CanonicalGrantee)grantee, xml); } else if (grantee instanceof EmailAddressGrantee) { return convertToXml((EmailAddressGrantee)grantee, xml); } else if (grantee instanceof GroupGrantee) { return convertToXml((GroupGrantee)grantee, xml); } else { throw new SdkClientException("Unknown Grantee type: " + grantee.getClass().getName()); } }
/** * Returns an XML fragment representing the specified group grantee. * * @param grantee * The group grantee to convert to an XML representation that can * be sent to Amazon S3 as part of request. * @param xml * The XmlWriter to which to concatenate this node to. * * @return The given XmlWriter containing the specified group grantee. */ protected XmlWriter convertToXml(GroupGrantee grantee, XmlWriter xml) { xml.start("Grantee", new String[] {"xmlns:xsi" , "xsi:type"}, new String[] {"http://www.w3.org/2001/XMLSchema-instance", "Group"}); xml.start("URI").value(grantee.getIdentifier()).end(); xml.end(); return xml; }