@Override
  public void signRequest(HttpUriRequest request) {
    AWSCredentials credentials = awsCredentialsProvider.getCredentials();
    if (credentials instanceof AWSSessionCredentials) {
      request.addHeader(
          SESSION_TOKEN_HEADER, ((AWSSessionCredentials) credentials).getSessionToken());
    }
    String canonicalRequest = createCanonicalRequest(request);
    log.debug("canonicalRequest: " + canonicalRequest);
    String[] requestParts = canonicalRequest.split("\n");
    String signedHeaders = requestParts[requestParts.length - 2];
    String stringToSign = createStringToSign(canonicalRequest);
    log.debug("stringToSign: " + stringToSign);
    String authScope = stringToSign.split("\n")[2];
    String signature = createSignature(stringToSign);

    String authHeader =
        String.format(
            AUTH_HEADER_FORMAT,
            credentials.getAWSAccessKeyId(),
            authScope,
            signedHeaders,
            signature);

    request.addHeader(AUTH_HEADER_NAME, authHeader);
  }
Example #2
0
 /**
  * Computes the presigned URL for the given S3 resource.
  *
  * @param path String like "/bucketName/folder/folder/abc.txt" that represents the resource to
  *     request.
  */
 public URL buildPresignedURL(String path) throws AmazonClientException {
   AWSCredentials credentials = awsCredentialsProvider.getCredentials();
   long expires = System.currentTimeMillis() + 60 * 60 * 1000;
   GeneratePresignedUrlRequest request =
       new GeneratePresignedUrlRequest(path, credentials.getAWSSecretKey());
   request.setExpiration(new Date(expires));
   AmazonS3 s3 = new AmazonS3Client(credentials);
   return s3.generatePresignedUrl(request);
 }
  public void sign(Request<T> request) throws SignatureException {
    String expirationInSeconds = Long.toString(expiration.getTime() / 1000L);

    String canonicalString =
        RestUtils.makeS3CanonicalString(httpVerb, resourcePath, request, expirationInSeconds);

    String secretKey;
    String accessKeyId;
    synchronized (credentials) {
      secretKey = credentials.getAWSSecretKey();
      accessKeyId = credentials.getAWSAccessKeyId();
    }

    String signature = super.sign(canonicalString, secretKey, SigningAlgorithm.HmacSHA1);

    request.addParameter("AWSAccessKeyId", accessKeyId);
    request.addParameter("Expires", expirationInSeconds);
    request.addParameter("Signature", signature);
  }
  public static void main(String[] args) throws Exception {
    credentials =
        new PropertiesCredentials(
            S3TransferProgressSample.class.getResourceAsStream("AwsCredentials.properties"));

    // TransferManager manages a pool of threads, so we create a
    // single instance and share it throughout our application.
    tx = new TransferManager(credentials);

    bucketName = "s3-upload-sdk-sample-" + credentials.getAWSAccessKeyId().toLowerCase();

    new S3TransferProgressSample();
  }