Example usage for com.amazonaws.services.s3 AmazonS3 getObjectMetadata

List of usage examples for com.amazonaws.services.s3 AmazonS3 getObjectMetadata

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3 getObjectMetadata.

Prototype

public ObjectMetadata getObjectMetadata(String bucketName, String key)
        throws SdkClientException, AmazonServiceException;

Source Link

Document

Gets the metadata for the specified Amazon S3 object without actually fetching the object itself.

Usage

From source file:c3.ops.priam.aws.S3FileSystem.java

License:Apache License

@Override
public void download(AbstractBackupPath path, OutputStream os) throws BackupRestoreException {
    try {/*from   ww  w .  ja va  2  s. c o m*/
        logger.info("Downloading " + path.getRemotePath());
        downloadCount.incrementAndGet();
        final AmazonS3 client = getS3Client();
        long contentLen = client.getObjectMetadata(getPrefix(), path.getRemotePath()).getContentLength();
        path.setSize(contentLen);
        RangeReadInputStream rris = new RangeReadInputStream(client, getPrefix(), path);
        final long bufSize = MAX_BUFFERED_IN_STREAM_SIZE > contentLen ? contentLen
                : MAX_BUFFERED_IN_STREAM_SIZE;
        compress.decompressAndClose(new BufferedInputStream(rris, (int) bufSize), os);
        bytesDownloaded.addAndGet(contentLen);
    } catch (Exception e) {
        throw new BackupRestoreException(e.getMessage(), e);
    }
}

From source file:com.openkm.util.backup.RepositoryS3Backup.java

License:Open Source License

/**
 * Performs a recursive repository content export with metadata
 *///  www .  ja  v a 2s . c o m
private static ImpExpStats backupHelper(String token, String fldPath, AmazonS3 s3, String bucket,
        boolean metadata, Writer out, InfoDecorator deco)
        throws FileNotFoundException, PathNotFoundException, AccessDeniedException, ParseException,
        NoSuchGroupException, RepositoryException, IOException, DatabaseException {
    log.info("backup({}, {}, {}, {}, {}, {})", new Object[] { token, fldPath, bucket, metadata, out, deco });
    ImpExpStats stats = new ImpExpStats();
    DocumentModule dm = ModuleManager.getDocumentModule();
    FolderModule fm = ModuleManager.getFolderModule();
    MetadataAdapter ma = MetadataAdapter.getInstance(token);
    Gson gson = new Gson();

    for (Iterator<Document> it = dm.getChildren(token, fldPath).iterator(); it.hasNext();) {
        File tmpDoc = null;
        InputStream is = null;
        FileOutputStream fos = null;
        boolean upload = true;

        try {
            Document docChild = it.next();
            String path = docChild.getPath().substring(1);
            ObjectMetadata objMeta = new ObjectMetadata();

            if (Config.REPOSITORY_CONTENT_CHECKSUM) {
                if (exists(s3, bucket, path)) {
                    objMeta = s3.getObjectMetadata(bucket, path);

                    if (docChild.getActualVersion().getChecksum().equals(objMeta.getETag())) {
                        upload = false;
                    }
                }
            }

            if (upload) {
                tmpDoc = FileUtils.createTempFileFromMime(docChild.getMimeType());
                fos = new FileOutputStream(tmpDoc);
                is = dm.getContent(token, docChild.getPath(), false);
                IOUtils.copy(is, fos);
                PutObjectRequest request = new PutObjectRequest(bucket, path, tmpDoc);

                if (metadata) {
                    // Metadata
                    DocumentMetadata dmd = ma.getMetadata(docChild);
                    String json = gson.toJson(dmd);
                    objMeta.addUserMetadata("okm", json);
                }

                request.setMetadata(objMeta);
                s3.putObject(request);
                out.write(deco.print(docChild.getPath(), docChild.getActualVersion().getSize(), null));
                out.flush();
            } else {
                if (metadata) {
                    // Metadata
                    DocumentMetadata dmd = ma.getMetadata(docChild);
                    String json = gson.toJson(dmd);
                    objMeta.addUserMetadata("okm", json);

                    // Update object metadata
                    CopyObjectRequest copyObjReq = new CopyObjectRequest(bucket, path, bucket, path);
                    copyObjReq.setNewObjectMetadata(objMeta);
                    s3.copyObject(copyObjReq);
                }

                log.info("Don't need to upload document {}", docChild.getPath());
            }

            // Stats
            stats.setSize(stats.getSize() + docChild.getActualVersion().getSize());
            stats.setDocuments(stats.getDocuments() + 1);
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(fos);
            FileUtils.deleteQuietly(tmpDoc);
        }
    }

    for (Iterator<Folder> it = fm.getChildren(token, fldPath).iterator(); it.hasNext();) {
        InputStream is = null;

        try {
            Folder fldChild = it.next();
            String path = fldChild.getPath().substring(1) + "/";
            is = new ByteArrayInputStream(new byte[0]);
            ObjectMetadata objMeta = new ObjectMetadata();
            objMeta.setContentLength(0);
            PutObjectRequest request = new PutObjectRequest(bucket, path, is, objMeta);

            // Metadata
            if (metadata) {
                FolderMetadata fmd = ma.getMetadata(fldChild);
                String json = gson.toJson(fmd);
                objMeta.addUserMetadata("okm", json);
            }

            request.setMetadata(objMeta);
            s3.putObject(request);

            ImpExpStats tmp = backupHelper(token, fldChild.getPath(), s3, bucket, metadata, out, deco);

            // Stats
            stats.setSize(stats.getSize() + tmp.getSize());
            stats.setDocuments(stats.getDocuments() + tmp.getDocuments());
            stats.setFolders(stats.getFolders() + tmp.getFolders() + 1);
            stats.setOk(stats.isOk() && tmp.isOk());
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    log.debug("backupHelper: {}", stats);
    return stats;
}

From source file:ecplugins.s3.S3Util.java

License:Apache License

public static boolean isValidFile(String bucketName, String path)
        throws AmazonClientException, AmazonServiceException, Exception {
    boolean isValidFile = true;
    Properties props = TestUtils.getProperties();

    BasicAWSCredentials credentials = new BasicAWSCredentials(props.getProperty(StringConstants.ACCESS_ID),
            props.getProperty(StringConstants.SECRET_ACCESS_ID));

    // Create TransferManager
    TransferManager tx = new TransferManager(credentials);

    // Get S3 Client
    AmazonS3 s3 = tx.getAmazonS3Client();

    try {/* ww w  . j a  v  a2  s  . c o  m*/
        ObjectMetadata objectMetadata = s3.getObjectMetadata(bucketName, path);
    } catch (AmazonS3Exception s3e) {
        if (s3e.getStatusCode() == 404) {
            // i.e. 404: NoSuchKey - The specified key does not exist
            isValidFile = false;
        } else {
            throw s3e; // rethrow all S3 exceptions other than 404
        }
    }

    return isValidFile;
}

From source file:it.openutils.mgnlaws.magnolia.datastore.S3LazyObject.java

License:Open Source License

public S3LazyObject(AmazonS3 client, String bucket, String key) {
    this.key = key;
    this.client = client;
    this.bucket = bucket;
    this.metadata = client.getObjectMetadata(bucket, key);
}

From source file:jp.classmethod.aws.gradle.s3.AbstractAmazonS3FileUploadTask.java

License:Apache License

protected ObjectMetadata existingObjectMetadata() {
    // to enable conventionMappings feature
    String bucketName = getBucketName();
    String key = getKey();/*from   w w w.  j av a  2 s. c  o  m*/

    AmazonS3PluginExtension ext = getProject().getExtensions().getByType(AmazonS3PluginExtension.class);
    AmazonS3 s3 = ext.getClient();

    try {
        // to enable conventionMapping, you must reference field via getters
        return s3.getObjectMetadata(bucketName, key);
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() != 404) {
            throw e;
        }
    }
    return null;
}

From source file:org.apache.beam.sdk.io.aws.s3.S3ReadableSeekableByteChannel.java

License:Apache License

S3ReadableSeekableByteChannel(AmazonS3 amazonS3, S3ResourceId path, S3Options options) throws IOException {
    this.amazonS3 = checkNotNull(amazonS3, "amazonS3");
    checkNotNull(path, "path");
    this.options = checkNotNull(options, "options");

    if (path.getSize().isPresent()) {
        contentLength = path.getSize().get();
        this.path = path;

    } else {/*from   ww  w .ja v  a2 s.  c  om*/
        try {
            contentLength = amazonS3.getObjectMetadata(path.getBucket(), path.getKey()).getContentLength();
        } catch (AmazonClientException e) {
            throw new IOException(e);
        }
        this.path = path.withSize(contentLength);
    }
}

From source file:org.applicationMigrator.migrationclient.FileTransferClient.java

License:Apache License

public boolean checkIfFileIsPresentOnServer(AmazonS3 s3Client, String bucketName, String destinationPathString)
        throws AmazonClientException, AmazonServiceException {
    boolean isValidFile = true;
    try {// www  .  j ava 2s  .  c  om
        s3Client.getObjectMetadata(bucketName, destinationPathString);
    } catch (AmazonS3Exception s3e) {
        if (s3e.getStatusCode() == 403) {
            isValidFile = false;
        } else {
            throw s3e; // rethrow all S3 exceptions other than 404
        }
    } catch (Exception e) {
        return false;
    }

    return isValidFile;
}

From source file:org.applicationMigrator.serverAgent.ServerAgentFileTransferClient.java

License:Apache License

public boolean checkIfFileIsPresentOnServer(AmazonS3 s3Client, String bucketName, String destinationPathString)
        throws AmazonClientException, AmazonServiceException {
    boolean isValidFile = true;
    try {/* w  w w . j ava  2s. c  o  m*/
        s3Client.getObjectMetadata(bucketName, destinationPathString);
    } catch (AmazonS3Exception s3e) {
        if (s3e.getStatusCode() == 403 || s3e.getStatusCode() == 404) {
            isValidFile = false;
        } else {
            throw s3e; // rethrow all S3 exceptions other than 404
        }
    } catch (Exception e) {
        return false;
    }

    return isValidFile;
}

From source file:org.finra.herd.dao.impl.S3OperationsImpl.java

License:Apache License

@Override
public ObjectMetadata getObjectMetadata(String s3BucketName, String s3Key, AmazonS3 s3Client) {
    return s3Client.getObjectMetadata(s3BucketName, s3Key);
}

From source file:org.xmlsh.aws.util.AWSUtil.java

License:BSD License

public static String getChecksum(AmazonS3 s3, S3Path path) {
    try {/* www .ja v  a2 s.  c o  m*/

        ObjectMetadata exists = s3.getObjectMetadata(path.getBucket(), path.getKey());
        if (exists != null)
            return exists.getETag();

    } catch (Exception e) {

    }
    return null;
}