/* (non-Javadoc)
   * @see com.amazonaws.services.s3.AmazonS3Client#initiateMultipartUpload(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest)
   */
  @Override
  public InitiateMultipartUploadResult initiateMultipartUpload(
      InitiateMultipartUploadRequest initiateMultipartUploadRequest)
      throws AmazonClientException, AmazonServiceException {

    appendUserAgent(initiateMultipartUploadRequest, USER_AGENT);

    // Generate a one-time use symmetric key and initialize a cipher to encrypt object data
    SecretKey envelopeSymmetricKey = EncryptionUtils.generateOneTimeUseSymmetricKey();
    Cipher symmetricCipher =
        EncryptionUtils.createSymmetricCipher(
            envelopeSymmetricKey, Cipher.ENCRYPT_MODE, cryptoConfig.getCryptoProvider(), null);

    if (cryptoConfig.getStorageMode() == CryptoStorageMode.ObjectMetadata) {
      EncryptionMaterials encryptionMaterials =
          encryptionMaterialsProvider.getEncryptionMaterials();
      // Encrypt the envelope symmetric key
      byte[] encryptedEnvelopeSymmetricKey =
          EncryptionUtils.getEncryptedSymmetricKey(
              envelopeSymmetricKey, encryptionMaterials, cryptoConfig.getCryptoProvider());

      // Store encryption info in metadata
      ObjectMetadata metadata =
          EncryptionUtils.updateMetadataWithEncryptionInfo(
              initiateMultipartUploadRequest,
              encryptedEnvelopeSymmetricKey,
              symmetricCipher,
              encryptionMaterials.getMaterialsDescription());

      // Update the request's metadata to the updated metadata
      initiateMultipartUploadRequest.setObjectMetadata(metadata);
    }

    InitiateMultipartUploadResult result =
        super.initiateMultipartUpload(initiateMultipartUploadRequest);
    EncryptedUploadContext encryptedUploadContext =
        new EncryptedUploadContext(
            initiateMultipartUploadRequest.getBucketName(),
            initiateMultipartUploadRequest.getKey(),
            envelopeSymmetricKey);
    encryptedUploadContext.setNextInitializationVector(symmetricCipher.getIV());
    encryptedUploadContext.setFirstInitializationVector(symmetricCipher.getIV());
    currentMultipartUploadSecretKeys.put(result.getUploadId(), encryptedUploadContext);

    return result;
  }
  /** @deprecated no longer used and will be removed in the future */
  @Deprecated
  public static ObjectMetadata updateMetadataWithEncryptionInfo(
      InitiateMultipartUploadRequest request,
      byte[] keyBytesToStoreInMetadata,
      Cipher symmetricCipher,
      Map<String, String> materialsDescription) {
    ObjectMetadata metadata = request.getObjectMetadata();
    if (metadata == null) metadata = new ObjectMetadata();

    updateMetadata(metadata, keyBytesToStoreInMetadata, symmetricCipher, materialsDescription);

    return metadata;
  }
Пример #3
0
  public static String xs3_init_multi_upload(String xs3_objname, int file_size, String file_type) {
    AWSCredentials xs3_credentials = new BasicAWSCredentials(xs3_access_key, xs3_secret_key);
    ClientConfiguration xs3_clientconfig = new ClientConfiguration();
    xs3_clientconfig.setProtocol(Protocol.HTTP);

    S3ClientOptions xs3_client_options = new S3ClientOptions();
    xs3_client_options.setPathStyleAccess(true);

    xs3_client = new AmazonS3Client(xs3_credentials, xs3_clientconfig);
    xs3_client.setEndpoint(xs3_endpoint);
    xs3_client.setS3ClientOptions(xs3_client_options);

    try {
      InitiateMultipartUploadRequest xs3_multi_req =
          new InitiateMultipartUploadRequest(xs3_bucketname, xs3_objname);
      xs3_multi_req.setCannedACL(CannedAccessControlList.PublicRead);
      ObjectMetadata xs3_meta = new ObjectMetadata();
      xs3_meta.setContentType(file_type);
      xs3_multi_req.setObjectMetadata(xs3_meta);

      InitiateMultipartUploadResult xs3_multi_res =
          xs3_client.initiateMultipartUpload(xs3_multi_req);

      String xs3_multi_uploadid = xs3_multi_res.getUploadId();

      String json_urls = gen_part_url(xs3_multi_uploadid, file_size, xs3_objname, file_type);
      return json_urls;

    } catch (AmazonServiceException ase) {
      System.out.println("xs3_svr_error_message:" + ase.getMessage());
      System.out.println("xs3_svr_status_code:  " + ase.getStatusCode());
      System.out.println("xs3_svr_error_code:   " + ase.getErrorCode());
      System.out.println("xs3_svr_error_type:   " + ase.getErrorType());
      System.out.println("xs3_svr_request_id:   " + ase.getRequestId());
    } catch (AmazonClientException ace) {
      System.out.println("xs3_clt_error_message:" + ace.getMessage());
    }
    return null;
  }
Пример #4
0
  @Override
  boolean keyCopied(ObjectMetadata sourceMetadata, AccessControlList objectAcl) {
    long objectSize = summary.getSize();
    MirrorOptions options = context.getOptions();
    String sourceBucketName = options.getSourceBucket();
    int maxPartRetries = options.getMaxRetries();
    String targetBucketName = options.getDestinationBucket();
    List<CopyPartResult> copyResponses = new ArrayList<CopyPartResult>();
    if (options.isVerbose()) {
      log.info("Initiating multipart upload request for " + summary.getKey());
    }
    InitiateMultipartUploadRequest initiateRequest =
        new InitiateMultipartUploadRequest(targetBucketName, keydest)
            .withObjectMetadata(sourceMetadata);

    if (options.isCrossAccountCopy()) {
      initiateRequest.withCannedACL(CannedAccessControlList.BucketOwnerFullControl);
    } else {
      initiateRequest.withAccessControlList(objectAcl);
    }

    InitiateMultipartUploadResult initResult = client.initiateMultipartUpload(initiateRequest);

    long partSize = options.getUploadPartSize();
    long bytePosition = 0;

    for (int i = 1; bytePosition < objectSize; i++) {
      long lastByte =
          bytePosition + partSize - 1 >= objectSize ? objectSize - 1 : bytePosition + partSize - 1;
      String infoMessage = "copying : " + bytePosition + " to " + lastByte;
      if (options.isVerbose()) {
        log.info(infoMessage);
      }
      CopyPartRequest copyRequest =
          new CopyPartRequest()
              .withDestinationBucketName(targetBucketName)
              .withDestinationKey(keydest)
              .withSourceBucketName(sourceBucketName)
              .withSourceKey(summary.getKey())
              .withUploadId(initResult.getUploadId())
              .withFirstByte(bytePosition)
              .withLastByte(lastByte)
              .withPartNumber(i);

      for (int tries = 1; tries <= maxPartRetries; tries++) {
        try {
          if (options.isVerbose()) log.info("try :" + tries);
          context.getStats().s3copyCount.incrementAndGet();
          CopyPartResult copyPartResult = client.copyPart(copyRequest);
          copyResponses.add(copyPartResult);
          if (options.isVerbose()) log.info("completed " + infoMessage);
          break;
        } catch (Exception e) {
          if (tries == maxPartRetries) {
            client.abortMultipartUpload(
                new AbortMultipartUploadRequest(
                    targetBucketName, keydest, initResult.getUploadId()));
            log.error("Exception while doing multipart copy", e);
            return false;
          }
        }
      }
      bytePosition += partSize;
    }
    CompleteMultipartUploadRequest completeRequest =
        new CompleteMultipartUploadRequest(
            targetBucketName, keydest, initResult.getUploadId(), getETags(copyResponses));
    client.completeMultipartUpload(completeRequest);
    if (options.isVerbose()) {
      log.info("completed multipart request for : " + summary.getKey());
    }
    context.getStats().bytesCopied.addAndGet(objectSize);
    return true;
  }