Search in sources :

Example 11 with S3Bucket

use of org.jets3t.service.model.S3Bucket in project tutorials by eugenp.

the class JetS3tLiveTest method givenStringUploaded_StringIsDownloaded.

@Test
public void givenStringUploaded_StringIsDownloaded() throws Exception {
    // Get a bucket
    S3Bucket bucket = createBucket();
    assertNotNull(bucket);
    uploadStringData();
    // Download
    S3Object stringObject = s3Service.getObject(BucketName, TestStringName);
    // Process stream into a string
    String downloadedString = new BufferedReader(new InputStreamReader(stringObject.getDataInputStream())).lines().collect(Collectors.joining("\n"));
    // Verify
    assertTrue(TestString.equals(downloadedString));
    // Clean up for next test
    deleteObject(TestStringName);
    deleteBucket();
}
Also used : S3Bucket(org.jets3t.service.model.S3Bucket) S3Object(org.jets3t.service.model.S3Object) Test(org.junit.Test)

Example 12 with S3Bucket

use of org.jets3t.service.model.S3Bucket in project tutorials by eugenp.

the class JetS3tLiveTest method givenBinaryFileUploaded_FileIsDownloaded.

@Test
public void givenBinaryFileUploaded_FileIsDownloaded() throws Exception {
    // get a bucket
    S3Bucket bucket = createBucket();
    assertNotNull(bucket);
    // Put a binary file
    S3Object fileObject = new S3Object(new File("src/test/resources/test.jpg"));
    s3Service.putObject(BucketName, fileObject);
    // Print info about type and name
    log.info("Content type:" + fileObject.getContentType());
    log.info("File object name is " + fileObject.getName());
    // Download
    S3Object newFileObject = s3Service.getObject(BucketName, "test.jpg");
    // Save to a different name
    File newFile = new File("src/test/resources/newtest.jpg");
    Files.copy(newFileObject.getDataInputStream(), newFile.toPath(), REPLACE_EXISTING);
    // Get hashes and compare
    String origMD5 = getFileMD5("src/test/resources/test.jpg");
    String newMD5 = getFileMD5("src/test/resources/newtest.jpg");
    assertTrue(origMD5.equals(newMD5));
    // Clean up
    deleteObject("test.jpg");
    deleteBucket();
}
Also used : S3Bucket(org.jets3t.service.model.S3Bucket) S3Object(org.jets3t.service.model.S3Object) Test(org.junit.Test)

Example 13 with S3Bucket

use of org.jets3t.service.model.S3Bucket in project tutorials by eugenp.

the class JetS3tLiveTest method givenString_Uploaded_StringInfoIsAvailable.

@Test
public void givenString_Uploaded_StringInfoIsAvailable() throws Exception {
    // Create a bucket
    S3Bucket bucket = createBucket();
    assertNotNull(bucket);
    // Upload a string
    uploadStringData();
    // Get the details
    StorageObject objectDetailsOnly = s3Service.getObjectDetails(BucketName, TestStringName);
    log.info("Content type: " + objectDetailsOnly.getContentType() + " length: " + objectDetailsOnly.getContentLength());
    // Delete it
    deleteObject(TestStringName);
    // For next test
    deleteBucket();
}
Also used : StorageObject(org.jets3t.service.model.StorageObject) S3Bucket(org.jets3t.service.model.S3Bucket) Test(org.junit.Test)

Example 14 with S3Bucket

use of org.jets3t.service.model.S3Bucket in project airavata by apache.

the class BucketsLoader method load.

/**
 * @param s3 S3Service
 * @param s3Tree S3Tree
 */
public void load(final S3Service s3, final S3Tree s3Tree) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            S3Bucket[] bucketArray;
            try {
                bucketArray = s3.listAllBuckets();
                for (S3Bucket s3Bucket : bucketArray) {
                    DefaultMutableTreeNode tempTreeNode = s3Tree.addObject((DefaultMutableTreeNode) null, s3Bucket.getName());
                    if (BucketsLoader.this.canceled)
                        return;
                    S3Object[] s3ObjectArray = s3.listObjects(s3Bucket.getName());
                    for (S3Object s3Object : s3ObjectArray) {
                        String keyName = s3Object.getName();
                        if (keyName.contains("$")) {
                            keyName = keyName.substring(0, keyName.indexOf('_'));
                        }
                        s3Tree.addObject(tempTreeNode, keyName);
                    }
                }
                s3Tree.refresh();
                if (bucketArray.length == 0) {
                    JOptionPane.showMessageDialog(BucketsLoader.this.parent, "Connection Failed!", "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                JOptionPane.showMessageDialog(BucketsLoader.this.parent, "Connection Successfully!", "Info", JOptionPane.INFORMATION_MESSAGE);
                // already connect
                S3TreeModel.getInstance().connect();
            } catch (S3ServiceException ex) {
                BucketsLoader.this.xbayaGUI.getErrorWindow().error(BucketsLoader.this.parent, "Cannot List S3 buckets", ex);
            } finally {
                BucketsLoader.this.loadingDialog.hide();
            }
        }
    }).start();
    this.loadingDialog.show();
}
Also used : S3Bucket(org.jets3t.service.model.S3Bucket) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) S3Object(org.jets3t.service.model.S3Object) S3ServiceException(org.jets3t.service.S3ServiceException)

Aggregations

S3Bucket (org.jets3t.service.model.S3Bucket)14 S3Object (org.jets3t.service.model.S3Object)9 Test (org.junit.Test)8 S3Service (org.jets3t.service.S3Service)2 S3ServiceException (org.jets3t.service.S3ServiceException)2 RestS3Service (org.jets3t.service.impl.rest.httpclient.RestS3Service)2 AWSCredentials (org.jets3t.service.security.AWSCredentials)2 FileIteratingFirehose (io.druid.data.input.impl.FileIteratingFirehose)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URI (java.net.URI)1 Date (java.util.Date)1 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)1 LineIterator (org.apache.commons.io.LineIterator)1 StorageObject (org.jets3t.service.model.StorageObject)1