private String generateUrl()

in teamcity-s3-sdk/src/main/java/jetbrains/buildServer/artifacts/s3/cloudfront/CloudFrontPresignedUrlProviderImpl.java [85:129]


  private String generateUrl(@NotNull String objectKey, @NotNull CloudFrontSettings settings, @NotNull Map<String, String> additionalParameters, @NotNull String distribution, int urlTtlSeconds)
    throws IOException {
    try {
      String domain = getDomainName(settings, distribution);
      String publicKeyId = settings.getCloudFrontPublicKeyId();

      String encodedObjectKey = SdkHttpUtils.urlEncodeIgnoreSlashes(objectKey);
      if (jetbrains.buildServer.util.StringUtil.isNotEmpty(domain) && StringUtil.isNotEmpty(publicKeyId)) {
        String resourcePath = CloudFrontUtils.generateResourcePath(CloudFrontUtils.Protocol.https, domain, encodedObjectKey);

        URIBuilder builder = new URIBuilder(resourcePath);

        if (!additionalParameters.isEmpty()) {
          for (Map.Entry<String, String> param : additionalParameters.entrySet()) {
            builder.addParameter(param.getKey(), param.getValue());
          }
        }

        resourcePath = builder.build().toString();

        byte[] privateKeyBytes = settings.getCloudFrontPrivateKey().getBytes(StandardCharsets.UTF_8);
        PrivateKey decodedPrivateKey = Pem.readPrivateKey(new ByteArrayInputStream(privateKeyBytes));

        final CannedSignerRequest request = CannedSignerRequest.builder()
                                                               .resourceUrl(resourcePath)
                                                               .privateKey(decodedPrivateKey)
                                                               .keyPairId(publicKeyId)
                                                               .expirationDate(new Date(myTimeService.now() + urlTtlSeconds * 1000L).toInstant())
                                                               .build();
        return CLOUD_FRONT_UTILITIES.getSignedUrlWithCannedPolicy(request).url();
      }
      return null;
    } catch (CloudFrontException | InvalidKeySpecException | IOException | URISyntaxException e) {
      final Throwable cause = e.getCause();
      final AWSException awsException = cause != null ? new AWSException(cause) : new AWSException(e);
      final String details = awsException.getDetails();
      if (StringUtil.isNotEmpty(details)) {
        final String message = awsException.getMessage() + details;
        LOG.warnAndDebugDetails(message, cause);
      }
      throw new IOException(
        String.format("Failed to create pre-signed URL to artifact '%s' in CloudFront distribution '%s': %s", objectKey, distribution, awsException.getMessage()),
        awsException);
    }
  }