@Test public void uploadFile() throws InterruptedException, ExecutionException { UploadedImage up = s3Conn.upload("activity", "mamadekadhis2", ImageRepository.ORIGINAL_CODE, ImageRepository.ORIGINAL_CODE, "jpg", new File("/home/ceefour/Pictures/Family/hendy mama dek adhis Trans Studio 388540_2728662699516_1538068978_n.jpg"), "image/jpeg").get(); log.info("Uploaded: {}", up); assertNotNull(up); }
@Override public ListenableFuture<UploadedImage> upload(final String namespace, final String imageId, final String styleCode, final String styleVariant, final String extension, final File file, final String contentType) { Preconditions.checkArgument(!Strings.isNullOrEmpty(contentType), "Cannot upload image without content type, namespace=%s imageId=%s styleCode=%s styleVariant=%s extension=%s file=%s", namespace, imageId, styleCode, styleVariant, extension, file); Preconditions.checkArgument(!Strings.isNullOrEmpty(extension), "Cannot upload image without extension, namespace=%s imageId=%s styleCode=%s styleVariant=%s file=%s contentType=%s", namespace, imageId, styleCode, styleVariant, file, contentType); final boolean useHi = ImageRepository.ORIGINAL_CODE.equals(styleCode); final String key = String.format("%s_%s/%s/%s/%s_%s.%s", tenantId, tenantEnv, namespace, styleCode, imageId, styleVariant, extension); final AccessControlList acl = new AccessControlList(); if (!Strings.isNullOrEmpty(canonicalUserId)) { acl.grantPermission(new CanonicalGrantee(canonicalUserId), Permission.FullControl); } acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); final PutObjectRequest putReq = new PutObjectRequest(bucket, key, file) .withAccessControlList(acl) .withStorageClass(useHi ? StorageClass.StandardInfrequentAccess : StorageClass.Standard); final Upload upload = transferMgr.upload(putReq); final String s3Uri = "s3: return executor.submit(new Callable<UploadedImage>() { @Override public UploadedImage call() throws Exception { try { final UploadResult uploadResult = upload.waitForUploadResult(); log.info("Uploaded '{}' to '{}', {} of {} bytes transferred: bucket={} key={}", file, s3Uri, upload.getProgress().getBytesTransferred(), file.length(), bucket, key ); final String realOriginAlias = Strings.isNullOrEmpty(originAlias) ? bucket + ".s3.amazonaws.com" : originAlias; final String realCdnAlias = Strings.isNullOrEmpty(cdnAlias) ? bucket + ".s3.amazonaws.com" : cdnAlias; final UploadedImage uploadedImage = ImageFactory.eINSTANCE.createUploadedImage(); uploadedImage.setOriginUri("http: uploadedImage.setUri("http: return uploadedImage; } catch (Exception e) { final String errorMessage = String.format("Failed upload '%s' to '%s' (%s) - %s of %s bytes transferred: %s", file, s3Uri, upload.getState(), upload.getProgress().getBytesTransferred(), file.length(), e); log.error(errorMessage, e); throw new ImageException(errorMessage, e); } } }); }
S3ConnectorImpl extends ImageConnectorImpl implements S3Connector { @Override public ListenableFuture<UploadedImage> upload(final String namespace, final String imageId, final String styleCode, final String styleVariant, final String extension, final File file, final String contentType) { Preconditions.checkArgument(!Strings.isNullOrEmpty(contentType), "Cannot upload image without content type, namespace=%s imageId=%s styleCode=%s styleVariant=%s extension=%s file=%s", namespace, imageId, styleCode, styleVariant, extension, file); Preconditions.checkArgument(!Strings.isNullOrEmpty(extension), "Cannot upload image without extension, namespace=%s imageId=%s styleCode=%s styleVariant=%s file=%s contentType=%s", namespace, imageId, styleCode, styleVariant, file, contentType); final boolean useHi = ImageRepository.ORIGINAL_CODE.equals(styleCode); final String key = String.format("%s_%s/%s/%s/%s_%s.%s", tenantId, tenantEnv, namespace, styleCode, imageId, styleVariant, extension); final AccessControlList acl = new AccessControlList(); if (!Strings.isNullOrEmpty(canonicalUserId)) { acl.grantPermission(new CanonicalGrantee(canonicalUserId), Permission.FullControl); } acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); final PutObjectRequest putReq = new PutObjectRequest(bucket, key, file) .withAccessControlList(acl) .withStorageClass(useHi ? StorageClass.StandardInfrequentAccess : StorageClass.Standard); final Upload upload = transferMgr.upload(putReq); final String s3Uri = "s3: return executor.submit(new Callable<UploadedImage>() { @Override public UploadedImage call() throws Exception { try { final UploadResult uploadResult = upload.waitForUploadResult(); log.info("Uploaded '{}' to '{}', {} of {} bytes transferred: bucket={} key={}", file, s3Uri, upload.getProgress().getBytesTransferred(), file.length(), bucket, key ); final String realOriginAlias = Strings.isNullOrEmpty(originAlias) ? bucket + ".s3.amazonaws.com" : originAlias; final String realCdnAlias = Strings.isNullOrEmpty(cdnAlias) ? bucket + ".s3.amazonaws.com" : cdnAlias; final UploadedImage uploadedImage = ImageFactory.eINSTANCE.createUploadedImage(); uploadedImage.setOriginUri("http: uploadedImage.setUri("http: return uploadedImage; } catch (Exception e) { final String errorMessage = String.format("Failed upload '%s' to '%s' (%s) - %s of %s bytes transferred: %s", file, s3Uri, upload.getState(), upload.getProgress().getBytesTransferred(), file.length(), e); log.error(errorMessage, e); throw new ImageException(errorMessage, e); } } }); } }
S3ConnectorImpl extends ImageConnectorImpl implements S3Connector { @Override public ListenableFuture<UploadedImage> upload(final String namespace, final String imageId, final String styleCode, final String styleVariant, final String extension, final File file, final String contentType) { Preconditions.checkArgument(!Strings.isNullOrEmpty(contentType), "Cannot upload image without content type, namespace=%s imageId=%s styleCode=%s styleVariant=%s extension=%s file=%s", namespace, imageId, styleCode, styleVariant, extension, file); Preconditions.checkArgument(!Strings.isNullOrEmpty(extension), "Cannot upload image without extension, namespace=%s imageId=%s styleCode=%s styleVariant=%s file=%s contentType=%s", namespace, imageId, styleCode, styleVariant, file, contentType); final boolean useHi = ImageRepository.ORIGINAL_CODE.equals(styleCode); final String key = String.format("%s_%s/%s/%s/%s_%s.%s", tenantId, tenantEnv, namespace, styleCode, imageId, styleVariant, extension); final AccessControlList acl = new AccessControlList(); if (!Strings.isNullOrEmpty(canonicalUserId)) { acl.grantPermission(new CanonicalGrantee(canonicalUserId), Permission.FullControl); } acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); final PutObjectRequest putReq = new PutObjectRequest(bucket, key, file) .withAccessControlList(acl) .withStorageClass(useHi ? StorageClass.StandardInfrequentAccess : StorageClass.Standard); final Upload upload = transferMgr.upload(putReq); final String s3Uri = "s3: return executor.submit(new Callable<UploadedImage>() { @Override public UploadedImage call() throws Exception { try { final UploadResult uploadResult = upload.waitForUploadResult(); log.info("Uploaded '{}' to '{}', {} of {} bytes transferred: bucket={} key={}", file, s3Uri, upload.getProgress().getBytesTransferred(), file.length(), bucket, key ); final String realOriginAlias = Strings.isNullOrEmpty(originAlias) ? bucket + ".s3.amazonaws.com" : originAlias; final String realCdnAlias = Strings.isNullOrEmpty(cdnAlias) ? bucket + ".s3.amazonaws.com" : cdnAlias; final UploadedImage uploadedImage = ImageFactory.eINSTANCE.createUploadedImage(); uploadedImage.setOriginUri("http: uploadedImage.setUri("http: return uploadedImage; } catch (Exception e) { final String errorMessage = String.format("Failed upload '%s' to '%s' (%s) - %s of %s bytes transferred: %s", file, s3Uri, upload.getState(), upload.getProgress().getBytesTransferred(), file.length(), e); log.error(errorMessage, e); throw new ImageException(errorMessage, e); } } }); } S3ConnectorImpl();  S3ConnectorImpl(String endpoint, Protocol protocol,
			String accessKey, String secretKey, String canonicalUserId,
			String bucket, String tenantId, String tenantEnv, String originAlias,
			String cdnAlias); }
S3ConnectorImpl extends ImageConnectorImpl implements S3Connector { @Override public ListenableFuture<UploadedImage> upload(final String namespace, final String imageId, final String styleCode, final String styleVariant, final String extension, final File file, final String contentType) { Preconditions.checkArgument(!Strings.isNullOrEmpty(contentType), "Cannot upload image without content type, namespace=%s imageId=%s styleCode=%s styleVariant=%s extension=%s file=%s", namespace, imageId, styleCode, styleVariant, extension, file); Preconditions.checkArgument(!Strings.isNullOrEmpty(extension), "Cannot upload image without extension, namespace=%s imageId=%s styleCode=%s styleVariant=%s file=%s contentType=%s", namespace, imageId, styleCode, styleVariant, file, contentType); final boolean useHi = ImageRepository.ORIGINAL_CODE.equals(styleCode); final String key = String.format("%s_%s/%s/%s/%s_%s.%s", tenantId, tenantEnv, namespace, styleCode, imageId, styleVariant, extension); final AccessControlList acl = new AccessControlList(); if (!Strings.isNullOrEmpty(canonicalUserId)) { acl.grantPermission(new CanonicalGrantee(canonicalUserId), Permission.FullControl); } acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); final PutObjectRequest putReq = new PutObjectRequest(bucket, key, file) .withAccessControlList(acl) .withStorageClass(useHi ? StorageClass.StandardInfrequentAccess : StorageClass.Standard); final Upload upload = transferMgr.upload(putReq); final String s3Uri = "s3: return executor.submit(new Callable<UploadedImage>() { @Override public UploadedImage call() throws Exception { try { final UploadResult uploadResult = upload.waitForUploadResult(); log.info("Uploaded '{}' to '{}', {} of {} bytes transferred: bucket={} key={}", file, s3Uri, upload.getProgress().getBytesTransferred(), file.length(), bucket, key ); final String realOriginAlias = Strings.isNullOrEmpty(originAlias) ? bucket + ".s3.amazonaws.com" : originAlias; final String realCdnAlias = Strings.isNullOrEmpty(cdnAlias) ? bucket + ".s3.amazonaws.com" : cdnAlias; final UploadedImage uploadedImage = ImageFactory.eINSTANCE.createUploadedImage(); uploadedImage.setOriginUri("http: uploadedImage.setUri("http: return uploadedImage; } catch (Exception e) { final String errorMessage = String.format("Failed upload '%s' to '%s' (%s) - %s of %s bytes transferred: %s", file, s3Uri, upload.getState(), upload.getProgress().getBytesTransferred(), file.length(), e); log.error(errorMessage, e); throw new ImageException(errorMessage, e); } } }); } S3ConnectorImpl();  S3ConnectorImpl(String endpoint, Protocol protocol,
			String accessKey, String secretKey, String canonicalUserId,
			String bucket, String tenantId, String tenantEnv, String originAlias,
			String cdnAlias); @Override String getCanonicalUserId(); @Override String getBucket(); @Override String getTenantId(); @Override String getTenantEnv(); @Override String getOriginAlias(); @Override String getCdnAlias(); @Override String getUriTemplate(); @Override ListenableFuture<UploadedImage> upload(final String namespace, final String imageId,
			final String styleCode, final String styleVariant, final String extension, final File file,
			final String contentType); @Override void delete(String namespace, String imageId, String styleCode,
			String styleVariant, String extension); @Override @PreDestroy void destroy(); @Override boolean download(String namespace, String imageId, String styleCode,
			String styleVariant, String extension, File file); @Override String getOriginUri(String namespace, String imageId,
			String styleCode, String styleVariant, String extension); }
S3ConnectorImpl extends ImageConnectorImpl implements S3Connector { @Override public ListenableFuture<UploadedImage> upload(final String namespace, final String imageId, final String styleCode, final String styleVariant, final String extension, final File file, final String contentType) { Preconditions.checkArgument(!Strings.isNullOrEmpty(contentType), "Cannot upload image without content type, namespace=%s imageId=%s styleCode=%s styleVariant=%s extension=%s file=%s", namespace, imageId, styleCode, styleVariant, extension, file); Preconditions.checkArgument(!Strings.isNullOrEmpty(extension), "Cannot upload image without extension, namespace=%s imageId=%s styleCode=%s styleVariant=%s file=%s contentType=%s", namespace, imageId, styleCode, styleVariant, file, contentType); final boolean useHi = ImageRepository.ORIGINAL_CODE.equals(styleCode); final String key = String.format("%s_%s/%s/%s/%s_%s.%s", tenantId, tenantEnv, namespace, styleCode, imageId, styleVariant, extension); final AccessControlList acl = new AccessControlList(); if (!Strings.isNullOrEmpty(canonicalUserId)) { acl.grantPermission(new CanonicalGrantee(canonicalUserId), Permission.FullControl); } acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); final PutObjectRequest putReq = new PutObjectRequest(bucket, key, file) .withAccessControlList(acl) .withStorageClass(useHi ? StorageClass.StandardInfrequentAccess : StorageClass.Standard); final Upload upload = transferMgr.upload(putReq); final String s3Uri = "s3: return executor.submit(new Callable<UploadedImage>() { @Override public UploadedImage call() throws Exception { try { final UploadResult uploadResult = upload.waitForUploadResult(); log.info("Uploaded '{}' to '{}', {} of {} bytes transferred: bucket={} key={}", file, s3Uri, upload.getProgress().getBytesTransferred(), file.length(), bucket, key ); final String realOriginAlias = Strings.isNullOrEmpty(originAlias) ? bucket + ".s3.amazonaws.com" : originAlias; final String realCdnAlias = Strings.isNullOrEmpty(cdnAlias) ? bucket + ".s3.amazonaws.com" : cdnAlias; final UploadedImage uploadedImage = ImageFactory.eINSTANCE.createUploadedImage(); uploadedImage.setOriginUri("http: uploadedImage.setUri("http: return uploadedImage; } catch (Exception e) { final String errorMessage = String.format("Failed upload '%s' to '%s' (%s) - %s of %s bytes transferred: %s", file, s3Uri, upload.getState(), upload.getProgress().getBytesTransferred(), file.length(), e); log.error(errorMessage, e); throw new ImageException(errorMessage, e); } } }); } S3ConnectorImpl();  S3ConnectorImpl(String endpoint, Protocol protocol,
			String accessKey, String secretKey, String canonicalUserId,
			String bucket, String tenantId, String tenantEnv, String originAlias,
			String cdnAlias); @Override String getCanonicalUserId(); @Override String getBucket(); @Override String getTenantId(); @Override String getTenantEnv(); @Override String getOriginAlias(); @Override String getCdnAlias(); @Override String getUriTemplate(); @Override ListenableFuture<UploadedImage> upload(final String namespace, final String imageId,
			final String styleCode, final String styleVariant, final String extension, final File file,
			final String contentType); @Override void delete(String namespace, String imageId, String styleCode,
			String styleVariant, String extension); @Override @PreDestroy void destroy(); @Override boolean download(String namespace, String imageId, String styleCode,
			String styleVariant, String extension, File file); @Override String getOriginUri(String namespace, String imageId,
			String styleCode, String styleVariant, String extension);  }