Search in sources :

Example 56 with Bucket

use of com.amazonaws.services.s3.model.Bucket in project cloudstack by apache.

the class NfsSecondaryStorageResource method s3ListVolume.

Map<Long, TemplateProp> s3ListVolume(S3TO s3) {
    String bucket = s3.getBucketName();
    // List the objects in the source directory on S3
    final List<S3ObjectSummary> objectSummaries = S3Utils.listDirectory(s3, bucket, VOLUME_ROOT_DIR);
    if (objectSummaries == null) {
        return null;
    }
    Map<Long, TemplateProp> tmpltInfos = new HashMap<Long, TemplateProp>();
    for (S3ObjectSummary objectSummary : objectSummaries) {
        String key = objectSummary.getKey();
        // String installPath = StringUtils.substringBeforeLast(key,
        // S3Utils.SEPARATOR);
        Long id = determineS3VolumeIdFromKey(key);
        // TODO: how to get volume template name
        TemplateProp tInfo = new TemplateProp(id.toString(), key, objectSummary.getSize(), objectSummary.getSize(), true, false);
        tmpltInfos.put(id, tInfo);
    }
    return tmpltInfos;
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) HashMap(java.util.HashMap) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary)

Example 57 with Bucket

use of com.amazonaws.services.s3.model.Bucket in project cloudstack by apache.

the class S3Utils method deleteDirectory.

public static void deleteDirectory(final ClientOptions clientOptions, final String bucketName, final String directoryName) {
    LOGGER.debug(format("Deleting S3 Directory %1$s in bucket %2$s", directoryName, bucketName));
    final List<S3ObjectSummary> objects = listDirectory(clientOptions, bucketName, directoryName);
    for (final S3ObjectSummary object : objects) {
        deleteObject(clientOptions, bucketName, object.getKey());
    }
    deleteObject(clientOptions, bucketName, directoryName);
}
Also used : S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary)

Example 58 with Bucket

use of com.amazonaws.services.s3.model.Bucket in project cloudstack by apache.

the class S3Utils method listDirectory.

public static List<S3ObjectSummary> listDirectory(final ClientOptions clientOptions, final String bucketName, final String directory) {
    LOGGER.debug(format("Listing S3 directory %1$s in bucket %2$s", directory, bucketName));
    List<S3ObjectSummary> objects = new ArrayList<>();
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
    listObjectsRequest.withBucketName(bucketName);
    listObjectsRequest.withPrefix(directory);
    ObjectListing ol = getAmazonS3Client(clientOptions).listObjects(listObjectsRequest);
    if (ol.isTruncated()) {
        do {
            objects.addAll(ol.getObjectSummaries());
            listObjectsRequest.setMarker(ol.getNextMarker());
            ol = getAmazonS3Client(clientOptions).listObjects(listObjectsRequest);
        } while (ol.isTruncated());
    } else {
        objects.addAll(ol.getObjectSummaries());
    }
    if (objects.isEmpty()) {
        return emptyList();
    }
    return unmodifiableList(objects);
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) ArrayList(java.util.ArrayList) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary)

Example 59 with Bucket

use of com.amazonaws.services.s3.model.Bucket in project exhibitor by soabase.

the class TestS3BackupProviderBase method testDownload.

@Test
public void testDownload() throws Exception {
    InputStream in = null;
    OutputStream out = null;
    File tempFile = File.createTempFile("test", ".test");
    try {
        in = new FileInputStream(sourceFile);
        PutObjectRequest dummyRequest = new PutObjectRequest("bucket", "exhibitor-backup" + S3BackupProvider.SEPARATOR + "test" + S3BackupProvider.SEPARATOR + 1, in, null);
        MockS3Client s3Client = new MockS3Client(null, null);
        s3Client.putObject(dummyRequest);
        S3BackupProvider provider = new S3BackupProvider(new MockS3ClientFactory(s3Client), new PropertyBasedS3Credential(new Properties()), new PropertyBasedS3ClientConfig(new Properties()), null);
        out = new FileOutputStream(tempFile);
        provider.downloadBackup(null, new BackupMetaData("test", 1), out, Maps.<String, String>newHashMap());
        Assert.assertEquals(Files.toByteArray(sourceFile), Files.toByteArray(tempFile));
    } finally {
        CloseableUtils.closeQuietly(in);
        CloseableUtils.closeQuietly(out);
        //noinspection ResultOfMethodCallIgnored
        tempFile.delete();
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BackupMetaData(com.netflix.exhibitor.core.backup.BackupMetaData) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) PropertyBasedS3ClientConfig(com.netflix.exhibitor.core.s3.PropertyBasedS3ClientConfig) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) PropertyBasedS3Credential(com.netflix.exhibitor.core.s3.PropertyBasedS3Credential) File(java.io.File) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.testng.annotations.Test)

Example 60 with Bucket

use of com.amazonaws.services.s3.model.Bucket in project exhibitor by soabase.

the class S3PseudoLock method createFile.

@Override
protected void createFile(String key, byte[] contents) throws Exception {
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(contents.length);
    PutObjectRequest request = new PutObjectRequest(bucket, key, new ByteArrayInputStream(contents), metadata);
    client.putObject(request);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)47 AmazonS3 (com.amazonaws.services.s3.AmazonS3)41 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)38 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)37 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)35 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)29 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)23 IOException (java.io.IOException)23 AmazonClientException (com.amazonaws.AmazonClientException)22 ArrayList (java.util.ArrayList)20 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)16 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)15 Test (org.junit.Test)15 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)13 S3Object (com.amazonaws.services.s3.model.S3Object)13 Path (org.apache.hadoop.fs.Path)12 Date (java.util.Date)11 Bucket (com.amazonaws.services.s3.model.Bucket)10 InputStream (java.io.InputStream)10 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)9