com.amazonaws.services.s3.model.GeneratePresignedUrlRequest Java Examples

The following examples show how to use com.amazonaws.services.s3.model.GeneratePresignedUrlRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: S3PreSignedUrlProviderImpl.java    From teamcity-s3-artifact-storage-plugin with Apache License 2.0 6 votes vote down vote up
@NotNull
private Callable<String> getUrlResolver(@NotNull final HttpMethod httpMethod,
                                        @NotNull final String bucketName,
                                        @NotNull final String objectKey, @NotNull final Map<String, String> params) {
  return () -> S3Util.withS3Client(ParamUtil.putSslValues(myServerPaths, params), client -> {
    final GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectKey, httpMethod)
      .withExpiration(new Date(System.currentTimeMillis() + getUrlLifetimeSec() * 1000));
    return IOGuard.allowNetworkCall(() -> {
      try {
        return client.generatePresignedUrl(request).toString();
      } catch (Throwable t) {
        if (t instanceof Exception) {
          throw (Exception)t;
        } else {
          throw new Exception(t);
        }
      }
    });
  });
}
 
Example #2
Source File: MockS3OperationsImpl.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc} <p/> <p> A mock implementation which generates a URL which reflects the given request. </p> <p> The URL is composed as such: </p> <p/>
 * <pre>
 * https://{s3BucketName}/{s3ObjectKey}?{queryParams}
 * </pre>
 * <p> Where {@code queryParams} is the URL encoded list of parameters given in the request. </p> <p> The query params include: </p> TODO: list the query
 * params in the result.
 */
@Override
public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest, AmazonS3 s3)
{
    String host = generatePresignedUrlRequest.getBucketName();
    StringBuilder file = new StringBuilder();
    file.append('/').append(generatePresignedUrlRequest.getKey());
    file.append("?method=").append(generatePresignedUrlRequest.getMethod());
    file.append("&expiration=").append(generatePresignedUrlRequest.getExpiration().getTime());
    try
    {
        return new URL("https", host, file.toString());
    }
    catch (MalformedURLException e)
    {
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: ContentHelper.java    From aws-photosharing-example with Apache License 2.0 5 votes vote down vote up
public URL getSignedUrl(String p_s3_bucket, String p_s3_file, Date p_exires) {    	   
  	
  	GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(p_s3_bucket, p_s3_file);
generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default.
generatePresignedUrlRequest.setExpiration(p_exires);
      
return s3Client.generatePresignedUrl(generatePresignedUrlRequest); 
  }
 
Example #4
Source File: AmazonS3Manager.java    From carina with Apache License 2.0 5 votes vote down vote up
/**
 * Method to generate pre-signed object URL to s3 object
 * 
 * @param bucketName AWS S3 bucket name
 * @param key (example: android/apkFolder/ApkName.apk)
 * @param ms espiration time in ms, i.e. 1 hour is 1000*60*60
 * @return url String pre-signed URL
 */
public URL generatePreSignUrl(final String bucketName, final String key, long ms) {

    java.util.Date expiration = new java.util.Date();
    long msec = expiration.getTime();
    msec += ms;
    expiration.setTime(msec);

    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key);
    generatePresignedUrlRequest.setMethod(HttpMethod.GET);
    generatePresignedUrlRequest.setExpiration(expiration);

    URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest);
    return url;
}
 
Example #5
Source File: Acl.java    From cloudExplorer with GNU General Public License v3.0 5 votes vote down vote up
String setACLurl(String object, String access_key, String secret_key, String endpoint, String bucket) {
    String URL = null;
    try {
        AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
        AmazonS3 s3Client = new AmazonS3Client(credentials,
                new ClientConfiguration());
         if (endpoint.contains("amazonaws.com")) {
            String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket));
            if (aws_endpoint.contains("US")) {
                s3Client.setEndpoint("https://s3.amazonaws.com");
            } else if (aws_endpoint.contains("us-west")) {
                s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
            } else if (aws_endpoint.contains("eu-west")) {
                s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
            } else if (aws_endpoint.contains("ap-")) {
                s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
            } else if (aws_endpoint.contains("sa-east-1")) {
                s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
            } else {
                s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com");
            }
        } else {
            s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
            s3Client.setEndpoint(endpoint);
        }
        java.util.Date expiration = new java.util.Date();
        long milliSeconds = expiration.getTime();
        milliSeconds += 1000 * 60 * 1000; // Add 1 hour.
        expiration.setTime(milliSeconds);
        GeneratePresignedUrlRequest generatePresignedUrlRequest
                = new GeneratePresignedUrlRequest(bucket, object);
        generatePresignedUrlRequest.setMethod(HttpMethod.GET);
        generatePresignedUrlRequest.setExpiration(expiration);
        URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
        URL = ("Pre-Signed URL = " + url.toString());
        StringSelection stringSelection = new StringSelection(url.toString());
    } catch (Exception setACLpublic) {
    }
    return URL;
}
 
Example #6
Source File: S3Storage.java    From digdag with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<DirectUploadHandle> getDirectUploadHandle(String key)
{
    final long secondsToExpire = config.get("direct_upload_expiration", Long.class, 10L*60);

    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucket, key);
    req.setMethod(HttpMethod.PUT);
    req.setExpiration(Date.from(Instant.now().plusSeconds(secondsToExpire)));

    String url = client.generatePresignedUrl(req).toString();

    return Optional.of(DirectUploadHandle.of(url));
}
 
Example #7
Source File: S3Storage.java    From digdag with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<DirectDownloadHandle> getDirectDownloadHandle(String key)
{
    final long secondsToExpire = config.get("direct_download_expiration", Long.class, 10L*60);

    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucket, key);
    req.setExpiration(Date.from(Instant.now().plusSeconds(secondsToExpire)));

    String url = client.generatePresignedUrl(req).toString();

    return Optional.of(DirectDownloadHandle.of(url));
}
 
Example #8
Source File: S3DaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
@Override
public String generateGetObjectPresignedUrl(String bucketName, String key, Date expiration, S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto)
{
    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
    generatePresignedUrlRequest.setExpiration(expiration);
    AmazonS3Client s3 = getAmazonS3(s3FileTransferRequestParamsDto);
    try
    {
        return s3Operations.generatePresignedUrl(generatePresignedUrlRequest, s3).toString();
    }
    finally
    {
        s3.shutdown();
    }
}
 
Example #9
Source File: S3Service.java    From modeldb with Apache License 2.0 5 votes vote down vote up
@Override
public String generatePresignedUrl(String s3Key, String method, long partNumber, String uploadId)
    throws ModelDBException {
  // Validate bucket
  Boolean exist = doesBucketExist(bucketName);
  if (!exist) {
    throw new ModelDBException("Bucket does not exists", io.grpc.Status.Code.INTERNAL);
  }

  HttpMethod reqMethod;
  if (method.equalsIgnoreCase(ModelDBConstants.PUT)) {
    reqMethod = HttpMethod.PUT;
  } else if (method.equalsIgnoreCase(ModelDBConstants.GET)) {
    reqMethod = HttpMethod.GET;
  } else {
    String errorMessage = "Unsupported HTTP Method for S3 Presigned URL";
    Status status =
        Status.newBuilder().setCode(Code.NOT_FOUND_VALUE).setMessage(errorMessage).build();
    LOGGER.info(errorMessage);
    throw StatusProto.toStatusRuntimeException(status);
  }

  // Set Expiration
  java.util.Date expiration = new java.util.Date();
  long milliSeconds = expiration.getTime();
  milliSeconds += 1000 * 60 * 5; // Add 5 mins
  expiration.setTime(milliSeconds);

  GeneratePresignedUrlRequest request =
      new GeneratePresignedUrlRequest(bucketName, s3Key)
          .withMethod(reqMethod)
          .withExpiration(expiration);
  if (partNumber != 0) {
    request.addRequestParameter("partNumber", String.valueOf(partNumber));
    request.addRequestParameter("uploadId", uploadId);
  }

  return s3Client.generatePresignedUrl(request).toString();
}
 
Example #10
Source File: GeneratePresignedPutUrl.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "To run this example, supply the name of an S3 bucket and a file to\n" +
            "upload to it.\n" +
            "\n" +
            "Ex: GeneratePresignedPutUrl <bucketname> <filename>\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String bucket_name = args[0];
    String key_name = args[1];

    System.out.format("Creating a pre-signed URL for uploading %s to S3 bucket %s...\n", key_name, bucket_name);
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();

    // Set the pre-signed URL to expire after 12 hours.
    java.util.Date expiration = new java.util.Date();
    long expirationInMs = expiration.getTime();
    expirationInMs += 1000 * 60 * 60 * 12;
    expiration.setTime(expirationInMs);

    try {
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket_name, key_name)
                .withMethod(HttpMethod.PUT)
                .withExpiration(expiration);
        URL url = s3.generatePresignedUrl(generatePresignedUrlRequest);
        //print URL
        System.out.println("\n\rGenerated URL: " + url.toString());
        //Print curl command to consume URL
        System.out.println("\n\rExample command to use URL for file upload: \n\r");
        System.out.println("curl --request PUT --upload-file /path/to/" + key_name + " '" + url.toString() + "' -# > /dev/null");
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}
 
Example #11
Source File: S3UrlGenerator.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
URL generateUrl(@NotNull String bucketName, @NotNull String objectKey, int expirationHours) {
    try {
        LOGGER.info("Generating pre-signed URL for bucket: {}\tobject: {}\texpirationTime: {} hours",
                bucketName,
                objectKey,
                expirationHours);
        long millisNow = System.currentTimeMillis();
        long expirationMillis = millisNow + 1000 * 60 * 60 * expirationHours;
        Date expiration = new java.util.Date(expirationMillis);
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey);
        generatePresignedUrlRequest.setMethod(HttpMethod.GET);
        generatePresignedUrlRequest.setExpiration(expiration);

        return s3Client().generatePresignedUrl(generatePresignedUrlRequest);
    } catch (AmazonServiceException exception) {
        LOGGER.error("Error Message: {}", exception.getMessage());
        LOGGER.error("HTTP  Code: {}", exception.getStatusCode());
        LOGGER.error("Error Code: {}", exception.getErrorCode());
        LOGGER.error("Error Type:   {}", exception.getErrorType());
        LOGGER.error("Request ID:   {}", exception.getRequestId());
    } catch (AmazonClientException ace) {
        LOGGER.error("The client encountered an internal error while trying to communicate with S3");
        LOGGER.error("Error Message: {}", ace.getMessage());
    }
    throw new IllegalStateException("Failed to create URL based on the S3 input configurations.");
}
 
Example #12
Source File: AwsFileSystem.java    From judgels with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getPrivateFileUrl(Path filePath) {
    String key = filePath.toString();
    return privateFileUrlCache.get(key, $ -> {
        GeneratePresignedUrlRequest presignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key);
        presignedUrlRequest.setExpiration(Date.from(Instant.now().plus(Duration.ofHours(4))));
        return s3.generatePresignedUrl(presignedUrlRequest).toString();
    });
}
 
Example #13
Source File: S3OperationsImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
@Override
public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest, AmazonS3 s3Client)
{
    return s3Client.generatePresignedUrl(generatePresignedUrlRequest);
}
 
Example #14
Source File: DummyS3Client.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** Unsupported Operation. */
@Override public URL generatePresignedUrl(
    GeneratePresignedUrlRequest generatePresignedUrlReq) throws SdkClientException {
    throw new UnsupportedOperationException("Operation not supported");
}
 
Example #15
Source File: AwsSdkTest.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
@Test
public void testAwsV2UrlSigningWithOverrideParameters() throws Exception {
    client = AmazonS3ClientBuilder.standard()
            .withClientConfiguration(V2_SIGNER_CONFIG)
            .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
            .withEndpointConfiguration(s3EndpointConfig).build();

    String blobName = "foo";
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(BYTE_SOURCE.size());
    client.putObject(containerName, blobName, BYTE_SOURCE.openStream(),
            metadata);

    GeneratePresignedUrlRequest generatePresignedUrlRequest =
            new GeneratePresignedUrlRequest(containerName, blobName);
    generatePresignedUrlRequest.setMethod(HttpMethod.GET);

    ResponseHeaderOverrides headerOverride = new ResponseHeaderOverrides();

    headerOverride.setContentDisposition("attachment; " + blobName);
    headerOverride.setContentType("text/plain");
    generatePresignedUrlRequest.setResponseHeaders(headerOverride);

    Date expiration = new Date(System.currentTimeMillis() +
            TimeUnit.HOURS.toMillis(1));
    generatePresignedUrlRequest.setExpiration(expiration);

    URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
    URLConnection connection =  url.openConnection();
    try (InputStream actual = connection.getInputStream();
         InputStream expected = BYTE_SOURCE.openStream()) {

        String value = connection.getHeaderField("Content-Disposition");
        assertThat(value).isEqualTo(headerOverride.getContentDisposition());

        value = connection.getHeaderField("Content-Type");
        assertThat(value).isEqualTo(headerOverride.getContentType());

        assertThat(actual).hasContentEqualTo(expected);
    }
}
 
Example #16
Source File: AmazonS3Mock.java    From Scribengin with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest) throws AmazonClientException {
  // TODO Auto-generated method stub
  return null;
}
 
Example #17
Source File: S3Operations.java    From herd with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a pre-signed URL for accessing an Amazon S3 resource.
 *
 * @param generatePresignedUrlRequest the request object containing all the options for generating a pre-signed URL (bucket name, key, expiration date,
 * etc)
 * @param s3Client the {@link AmazonS3} implementation to use
 *
 * @return the pre-signed URL which expires at the specified time, and can be used to allow anyone to download the specified object from S3, without
 *         exposing the owner's AWS secret access key
 */
public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest, AmazonS3 s3Client);