Search in sources :

Example 36 with S3Object

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

the class JetS3tLiveTest method uploadStringData.

private void uploadStringData() throws Exception {
    S3Object stringObject = new S3Object(TestStringName, TestString);
    s3Service.putObject(BucketName, stringObject);
    log.info("Content type:" + stringObject.getContentType());
}
Also used : S3Object(org.jets3t.service.model.S3Object)

Example 37 with S3Object

use of org.jets3t.service.model.S3Object 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)

Example 38 with S3Object

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

the class S3Downloader method download.

/**
 * Download bucket.
 *
 * @param s3 S3Service
 * @param bucket bucket
 * @param key Key
 * @param directory directory
 */
public void download(final S3Service s3, final String bucket, final String key, final String directory) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            BufferedWriter out = null;
            BufferedReader in = null;
            try {
                S3Object s3Object = s3.getObject(bucket, key);
                File fileOut = new File(directory + File.separator + s3Object.getKey());
                if (!fileOut.getParentFile().exists()) {
                    fileOut.getParentFile().mkdirs();
                }
                if (!fileOut.exists()) {
                    fileOut.createNewFile();
                }
                out = new BufferedWriter(new FileWriter(fileOut));
                in = new BufferedReader(new InputStreamReader(s3Object.getDataInputStream()));
                String data = null;
                while ((data = in.readLine()) != null) {
                    // stop download and delete file
                    if (S3Downloader.this.canceled) {
                        out.close();
                        fileOut.delete();
                        return;
                    }
                    out.write(data);
                    out.newLine();
                }
                S3Downloader.this.engine.getGUI().getErrorWindow().info(S3Downloader.this.parent, "", "Downloaded successfully!");
            } catch (Exception ex) {
                S3Downloader.this.engine.getGUI().getErrorWindow().error(S3Downloader.this.parent, "Download failed! Please ensure every fields are filled correctly", ex);
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException io) {
                    // do nothing
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException io) {
                    // do nothing
                    }
                }
                // close loading dialog
                S3Downloader.this.loadingDialog.hide();
            }
        }
    }).start();
    this.loadingDialog.show();
}
Also used : S3Object(org.jets3t.service.model.S3Object)

Example 39 with S3Object

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

the class S3Uploader method upload.

/**
 * Upload bucket.
 *
 * @param s3 S3Service
 * @param s3tree S3Tree
 * @param bucket bucket
 * @param filePath file path
 */
public void upload(final S3Service s3, final S3Tree s3tree, final String bucket, final String filePath) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            int index;
            index = filePath.lastIndexOf('/');
            String fileName;
            if (index == -1) {
                index = filePath.lastIndexOf('\\');
            }
            fileName = filePath.substring(index + 1, filePath.length());
            try {
                S3Object s3Object = new S3Object(new File(filePath));
                s3.putObject(bucket, s3Object);
                /*
                     * We cannot cancel during upload, so delete file instead
                     */
                if (S3Uploader.this.canceled) {
                    s3.deleteObject(bucket, s3Object.getKey());
                } else {
                    S3Uploader.this.engine.getGUI().getErrorWindow().info(S3Uploader.this.parent, "", "Uploaded successfully!");
                    // add key to S3Tree
                    int startIndex = bucket.lastIndexOf('/');
                    startIndex = startIndex >= 0 ? startIndex : 0;
                    if (startIndex != 0) {
                        fileName = bucket.substring(startIndex) + '/' + fileName;
                    }
                    if (fileName.startsWith("/")) {
                        fileName = fileName.substring(1, fileName.length());
                    }
                    s3tree.addObject(bucket, fileName);
                }
            } catch (Exception ex) {
                S3Uploader.this.engine.getGUI().getErrorWindow().error(S3Uploader.this.parent, "Upload failed! Please ensure every fields are filled correctly", ex);
            } finally {
                // close loading dialog
                S3Uploader.this.loadingDialog.hide();
            }
        }
    }).start();
    this.loadingDialog.show();
}
Also used : S3Object(org.jets3t.service.model.S3Object) File(java.io.File)

Aggregations

S3Object (org.jets3t.service.model.S3Object)39 Test (org.junit.Test)16 S3ServiceException (org.jets3t.service.S3ServiceException)11 ServiceException (org.jets3t.service.ServiceException)11 RestS3Service (org.jets3t.service.impl.rest.httpclient.RestS3Service)11 S3Bucket (org.jets3t.service.model.S3Bucket)9 Date (java.util.Date)8 File (java.io.File)6 IOException (java.io.IOException)6 DataSegment (io.druid.timeline.DataSegment)5 URI (java.net.URI)5 StorageObject (org.jets3t.service.model.StorageObject)5 InputStream (java.io.InputStream)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 Pattern (java.util.regex.Pattern)3 S3Service (org.jets3t.service.S3Service)3 AWSCredentials (org.jets3t.service.security.AWSCredentials)3 FileUtils (io.druid.java.util.common.FileUtils)2 BufferedReader (java.io.BufferedReader)2