public void uploadSnapshot(
     File snapshotFile, SnapshotProgressCallback callback, String snapshotKey, String snapshotId)
     throws EucalyptusCloudException {
   try {
     s3Client.getS3Client().listObjects(StorageProperties.SNAPSHOT_BUCKET);
   } catch (Exception ex) {
     try {
       // if (!s3Client.getS3Client().doesBucketExist(StorageProperties.SNAPSHOT_BUCKET)) {
       s3Client.getS3Client().createBucket(StorageProperties.SNAPSHOT_BUCKET);
       // }
     } catch (Exception e) {
       LOG.error("Snapshot upload failed. Unable to create bucket: snapshots", e);
       throw new EucalyptusCloudException(e);
     }
   }
   try {
     ObjectMetadata metadata = new ObjectMetadata();
     metadata.setContentLength(snapshotFile.length());
     // FIXME: need to set MD5
     s3Client
         .getS3Client()
         .putObject(
             StorageProperties.SNAPSHOT_BUCKET,
             snapshotKey,
             new FileInputStreamWithCallback(snapshotFile, callback),
             metadata);
   } catch (Exception ex) {
     LOG.error("Snapshot " + snapshotId + " upload failed to: " + snapshotKey, ex);
     throw new EucalyptusCloudException(ex);
   }
 }
 public SnapshotObjectOps(CredentialsType credentials) {
   s3Client =
       new S3Client(
           new BasicSessionCredentials(
               credentials.getAccessKeyId(),
               credentials.getSecretAccessKey(),
               credentials.getSessionToken()),
           false);
   s3Client.setUsePathStyle(true);
   s3Client.setS3Endpoint(StorageProperties.WALRUS_URL);
 }
 public void deleteSnapshot(String snapshotLocation, String snapshotId)
     throws EucalyptusCloudException {
   try {
     s3Client.getS3Client().deleteObject(StorageProperties.SNAPSHOT_BUCKET, snapshotLocation);
   } catch (Exception ex) {
     LOG.error("Snapshot delete failed for: " + snapshotId, ex);
     throw new EucalyptusCloudException(ex);
   }
   try {
     s3Client.getS3Client().deleteBucket(StorageProperties.SNAPSHOT_BUCKET);
   } catch (Exception ex) {
     LOG.error("Snapshot bucket delete failed for: " + StorageProperties.SNAPSHOT_BUCKET, ex);
     throw new EucalyptusCloudException(ex);
   }
 }
 public void createBucket(String bucket) throws EucalyptusCloudException {
   refreshOsgURI();
   try {
     s3Client.getS3Client().createBucket(bucket);
   } catch (Exception ex) {
     LOG.error("Failed to create bucket: " + bucket, ex);
     throw new EucalyptusCloudException("Failed to create bucket: " + bucket, ex);
   }
 }
 public void deleteSnapshot(String bucket, String key) throws EucalyptusCloudException {
   refreshOsgURI();
   try {
     s3Client.getS3Client().deleteObject(bucket, key);
   } catch (Exception ex) {
     LOG.error("Failed to delete snapshot file with key " + key, ex);
     throw new EucalyptusCloudException(ex);
   }
 }
 /** Utility method for refreshing OSG URI in the S3Client object used by this class. */
 private void refreshOsgURI() {
   try {
     ServiceConfiguration osgConfig = getOsgConfig();
     String osgURI = ServiceUris.remote(osgConfig).toASCIIString();
     s3Client.setS3Endpoint(osgURI);
     LOG.info("Setting objectstorage URI to: " + osgURI);
   } catch (Exception e) {
     LOG.warn("Could not refresh objectstorage URI");
   }
 }
 public long getSnapshotSize(String bucket, String key) throws EucalyptusCloudException {
   refreshOsgURI();
   try {
     ObjectMetadata snapshotMetadata = s3Client.getS3Client().getObjectMetadata(bucket, key);
     return snapshotMetadata.getContentLength();
   } catch (Exception ex) {
     LOG.error("Snapshot download failed for: ", ex);
     throw new EucalyptusCloudException(ex);
   }
 }
 public Boolean checkSnapshotExists(String bucket, String key) throws EucalyptusCloudException {
   refreshOsgURI();
   try {
     s3Client.getS3Client().getObjectMetadata(bucket, key);
     return Boolean.TRUE;
   } catch (AmazonServiceException aex) {
     // May be key off on error code for objectnotfound?
     return Boolean.FALSE;
   } catch (Exception ex) {
     LOG.error("Snapshot download failed for: ", ex);
     throw new EucalyptusCloudException(ex);
   }
 }
 public SnapshotObjectOps() {
   try {
     Account system = Accounts.lookupAccountByName(Account.SYSTEM_ACCOUNT);
     for (User user : system.getUsers()) {
       if (StorageProperties.BLOCKSTORAGE_ACCOUNT.equals(user.getName())) {
         List<AccessKey> keys = user.getKeys();
         if (!keys.isEmpty()) {
           AccessKey key = keys.get(0);
           s3Client =
               new S3Client(
                   new BasicAWSCredentials(key.getAccessKey(), key.getSecretKey()), false);
           s3Client.setUsePathStyle(true);
           s3Client.setS3Endpoint(StorageProperties.WALRUS_URL);
         } else {
           LOG.error(
               "Something went really wrong. Block storage account does not have an associated access key.");
         }
       }
     }
   } catch (Exception ex) {
     LOG.error(ex, ex);
   }
 }
 public void cancelSnapshotUpload(String snapshotId, String bucket, String key)
     throws EucalyptusCloudException {
   refreshOsgURI();
   try {
     if (checkSnapshotExists(bucket, key)) {
       deleteSnapshot(bucket, key);
     } else {
       new SnapshotUploader(snapshotId, bucket, key, s3Client.getS3Client()).cancelUpload();
     }
   } catch (Exception ex) {
     LOG.debug("Failed to cancel upload for snapshot " + snapshotId, ex);
     throw new EucalyptusCloudException(ex);
   }
 }
 public void downloadSnapshot(
     String snapshotBucket, String snapshotLocation, File tmpCompressedFile)
     throws EucalyptusCloudException {
   GetObjectRequest getObjectRequest = new GetObjectRequest(snapshotBucket, snapshotLocation);
   try {
     long startTime = System.currentTimeMillis();
     s3Client.getS3Client().getObject(getObjectRequest, tmpCompressedFile);
     LOG.info(
         "Snapshot "
             + snapshotBucket
             + "/"
             + snapshotLocation
             + " download took "
             + Long.toString(System.currentTimeMillis() - startTime)
             + "ms");
   } catch (Exception ex) {
     LOG.error("Snapshot download failed for: " + snapshotLocation, ex);
     throw new EucalyptusCloudException(ex);
   }
 }
 public void downloadSnapshot(String bucket, String key, File snapshotFile)
     throws EucalyptusCloudException {
   refreshOsgURI();
   GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
   try {
     long startTime = System.currentTimeMillis();
     s3Client.getS3Client().getObject(getObjectRequest, snapshotFile);
     LOG.info(
         "Snapshot "
             + bucket
             + "/"
             + key
             + " download took "
             + Long.toString(System.currentTimeMillis() - startTime)
             + "ms");
   } catch (Exception ex) {
     LOG.error("Snapshot download failed for: " + key, ex);
     throw new EucalyptusCloudException(ex);
   }
 }
 // TODO EUCA-8700 - Temporary workaround for snapshot uploads to work. THIS CANNOT BE RELEASED
 public SnapshotObjectOps(String accessKey, String secretKey) {
   s3Client = new S3Client(new BasicAWSCredentials(accessKey, secretKey), false);
   s3Client.setUsePathStyle(true);
   s3Client.setS3Endpoint(StorageProperties.WALRUS_URL);
 }
 public void uploadSnapshot(String snapshotId, String bucket, String key, String snapshotFileName)
     throws EucalyptusCloudException {
   refreshOsgURI();
   new SnapshotUploader(snapshotId, bucket, key, snapshotFileName, s3Client.getS3Client())
       .upload();
 }