Example usage for com.amazonaws.services.s3.model ObjectMetadata getContentLength

List of usage examples for com.amazonaws.services.s3.model ObjectMetadata getContentLength

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model ObjectMetadata getContentLength.

Prototype

public long getContentLength() 

Source Link

Document

<p> Gets the Content-Length HTTP header indicating the size of the associated object in bytes.

Usage

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

License:Apache License

@Override
public void validateS3File(S3FileTransferRequestParamsDto params, Long fileSizeInBytes)
        throws RuntimeException {
    ObjectMetadata objectMetadata = getObjectMetadata(params);

    if (objectMetadata == null) {
        throw new ObjectNotFoundException(String.format("File not found at s3://%s/%s location.",
                params.getS3BucketName(), params.getS3KeyPrefix()));
    }// w w  w .  jav a 2  s  .c o m

    Assert.isTrue(fileSizeInBytes == null || Objects.equals(fileSizeInBytes, objectMetadata.getContentLength()),
            String.format(
                    "Specified file size (%d bytes) does not match to the actual file size (%d bytes) reported by S3 for s3://%s/%s file.",
                    fileSizeInBytes, objectMetadata.getContentLength(), params.getS3BucketName(),
                    params.getS3KeyPrefix()));
}

From source file:org.geowebcache.s3.S3BlobStore.java

License:Open Source License

@Override
public void put(TileObject obj) throws StorageException {
    final Resource blob = obj.getBlob();
    checkNotNull(blob);/*from  ww  w .  j a  v a2  s .c o  m*/
    checkNotNull(obj.getBlobFormat());

    final String key = keyBuilder.forTile(obj);
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(blob.getSize());

    String blobFormat = obj.getBlobFormat();
    String mimeType;
    try {
        mimeType = MimeType.createFromFormat(blobFormat).getMimeType();
    } catch (MimeException me) {
        throw Throwables.propagate(me);
    }
    objectMetadata.setContentType(mimeType);

    // don't bother for the extra call if there are no listeners
    final boolean existed;
    ObjectMetadata oldObj;
    if (listeners.isEmpty()) {
        existed = false;
        oldObj = null;
    } else {
        oldObj = s3Ops.getObjectMetadata(key);
        existed = oldObj != null;
    }

    final ByteArrayInputStream input = toByteArray(blob);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, input, objectMetadata)
            .withCannedAcl(CannedAccessControlList.PublicRead);

    log.trace(log.isTraceEnabled() ? ("Storing " + key) : "");
    s3Ops.putObject(putObjectRequest);

    putParametersMetadata(obj.getLayerName(), obj.getParametersId(), obj.getParameters());

    /*
     * This is important because listeners may be tracking tile existence
     */
    if (!listeners.isEmpty()) {
        if (existed) {
            long oldSize = oldObj.getContentLength();
            listeners.sendTileUpdated(obj, oldSize);
        } else {
            listeners.sendTileStored(obj);
        }
    }
}

From source file:org.geowebcache.s3.S3BlobStore.java

License:Open Source License

@Override
public boolean delete(TileObject obj) throws StorageException {
    final String key = keyBuilder.forTile(obj);

    // don't bother for the extra call if there are no listeners
    if (listeners.isEmpty()) {
        return s3Ops.deleteObject(key);
    }/*from   w  w w.jav a  2s . co  m*/

    ObjectMetadata oldObj = s3Ops.getObjectMetadata(key);

    if (oldObj == null) {
        return false;
    }

    s3Ops.deleteObject(key);
    obj.setBlobSize((int) oldObj.getContentLength());
    listeners.sendTileDeleted(obj);
    return true;
}

From source file:org.gradle.internal.resource.transport.aws.s3.S3ResourceConnector.java

License:Apache License

public ExternalResourceMetaData getMetaData(URI location) {
    LOGGER.debug("Attempting to get resource metadata: {}", location);
    S3Object s3Object = s3Client.getMetaData(location);
    if (s3Object == null) {
        return null;
    }//  ww  w.j  av a  2 s.  c o  m
    ObjectMetadata objectMetadata = s3Object.getObjectMetadata();
    return new DefaultExternalResourceMetaData(location, objectMetadata.getLastModified().getTime(),
            objectMetadata.getContentLength(), objectMetadata.getContentType(), objectMetadata.getETag(), null); // Passing null for sha1 - TODO - consider using the etag which is an MD5 hash of the file (when less than 5Gb)
}

From source file:org.gytheio.content.handler.s3.S3ContentReferenceHandlerImpl.java

License:Open Source License

@Override
public long putInputStream(InputStream sourceInputStream, ContentReference targetContentReference)
        throws ContentIOException {
    if (!isContentReferenceSupported(targetContentReference)) {
        throw new ContentIOException("ContentReference not supported");
    }/*w  w w .java  2 s.  co  m*/

    String remotePath = getRelativePath(targetContentReference.getUri());

    try {
        s3.putObject(new PutObjectRequest(s3BucketName, remotePath, sourceInputStream, new ObjectMetadata()));
        ObjectMetadata metadata = s3.getObjectMetadata(new GetObjectMetadataRequest(s3BucketName, remotePath));
        return metadata.getContentLength();
    } catch (AmazonClientException e) {
        throw new ContentIOException("Failed to write content", e);
    }
}

From source file:org.opendedup.sdfs.filestore.cloud.BatchAwsS3ChunkStore.java

License:Open Source License

private void getData(long id, File f) throws Exception {
    // SDFSLogger.getLog().info("Downloading " + id);
    // SDFSLogger.getLog().info("Current readers :" + rr.incrementAndGet());
    String haName = EncyptUtils.encHashArchiveName(id, Main.chunkStoreEncryptionEnabled);
    this.s3clientLock.readLock().lock();
    S3Object sobj = null;//from   w w w .  j a v a 2  s  .c  o  m
    try {

        long tm = System.currentTimeMillis();
        ObjectMetadata omd = s3Service.getObjectMetadata(this.name, "blocks/" + haName);

        try {
            sobj = s3Service.getObject(this.name, "blocks/" + haName);
        } catch (Exception e) {
            throw new IOException(e);
        }
        int cl = (int) omd.getContentLength();
        if (this.simpleS3) {

            FileOutputStream out = null;
            InputStream in = null;
            try {
                out = new FileOutputStream(f);
                in = sobj.getObjectContent();
                IOUtils.copy(in, out);
                out.flush();

            } catch (Exception e) {
                throw new IOException(e);
            } finally {
                IOUtils.closeQuietly(out);
                IOUtils.closeQuietly(in);

            }
        } else {
            this.multiPartDownload("blocks/" + haName, f);
        }
        double dtm = (System.currentTimeMillis() - tm) / 1000d;
        double bps = (cl / 1024) / dtm;
        SDFSLogger.getLog().debug("read [" + id + "] at " + bps + " kbps");
        Map<String, String> mp = this.getUserMetaData(omd);
        if (md5sum && mp.containsKey("md5sum")) {
            byte[] shash = BaseEncoding.base64().decode(mp.get("md5sum"));

            InputStream in = new FileInputStream(f);
            byte[] chash = ServiceUtils.computeMD5Hash(in);
            IOUtils.closeQuietly(in);
            if (!Arrays.equals(shash, chash))
                throw new IOException("download corrupt at " + id);
        }

        try {
            mp.put("lastaccessed", Long.toString(System.currentTimeMillis()));
            omd.setUserMetadata(mp);

            updateObject("blocks/" + haName, omd);
        } catch (Exception e) {
            SDFSLogger.getLog().debug("error setting last accessed", e);
        }
        if (mp.containsKey("deleted")) {
            boolean del = Boolean.parseBoolean((String) mp.get("deleted"));
            if (del) {
                S3Object kobj = s3Service.getObject(this.name, "keys/" + haName);

                int claims = this.getClaimedObjects(kobj, id);

                int delobj = 0;
                if (mp.containsKey("deleted-objects")) {
                    delobj = Integer.parseInt((String) mp.get("deleted-objects")) - claims;
                    if (delobj < 0)
                        delobj = 0;
                }
                mp.remove("deleted");
                mp.put("deleted-objects", Integer.toString(delobj));
                mp.put("suspect", "true");
                omd.setUserMetadata(mp);

                updateObject("keys/" + haName, omd);
                int _size = Integer.parseInt((String) mp.get("size"));
                int _compressedSize = Integer.parseInt((String) mp.get("compressedsize"));
                HashBlobArchive.currentLength.addAndGet(_size);
                HashBlobArchive.compressedLength.addAndGet(_compressedSize);
                SDFSLogger.getLog().warn("Reclaimed [" + claims + "] blocks marked for deletion");
                kobj.close();
            }
        }
        dtm = (System.currentTimeMillis() - tm) / 1000d;
        bps = (cl / 1024) / dtm;
    } catch (AmazonS3Exception e) {
        if (e.getErrorCode().equalsIgnoreCase("InvalidObjectState"))
            throw new DataArchivedException(id, null);
        else {
            SDFSLogger.getLog().error("unable to get block [" + id + "] at [blocks/" + haName + "]", e);
            throw e;

        }
    } finally {
        try {
            if (sobj != null) {
                sobj.close();
            }
        } catch (Exception e) {

        }
        this.s3clientLock.readLock().unlock();
    }
}

From source file:org.opendedup.sdfs.filestore.cloud.BatchAwsS3ChunkStore.java

License:Open Source License

public StringResult getStringResult(String key) throws IOException, InterruptedException {
    this.s3clientLock.readLock().lock();
    S3Object sobj = null;/*www . j  ava2 s  . co  m*/
    try {

        ObjectMetadata md = null;
        try {
            sobj = s3Service.getObject(getName(), key);
            md = s3Service.getObjectMetadata(this.name, key);
        } catch (Exception e) {
            throw new IOException(e);
        }
        int cl = (int) md.getContentLength();

        byte[] data = new byte[cl];
        DataInputStream in = null;
        try {
            in = new DataInputStream(sobj.getObjectContent());
            in.readFully(data);

        } catch (Exception e) {
            throw new IOException(e);
        } finally {
            if (in != null)
                in.close();
        }
        boolean encrypt = false;
        boolean compress = false;
        boolean lz4compress = false;
        Map<String, String> mp = this.getUserMetaData(md);
        byte[] ivb = null;
        if (mp.containsKey("ivspec")) {
            ivb = BaseEncoding.base64().decode(mp.get("ivspec"));
        }
        if (mp.containsKey("md5sum")) {
            try {
                byte[] shash = BaseEncoding.base64().decode(mp.get("md5sum"));
                byte[] chash = ServiceUtils.computeMD5Hash(data);
                if (!Arrays.equals(shash, chash))
                    throw new IOException("download corrupt at " + sobj.getKey());
            } catch (NoSuchAlgorithmException e) {
                throw new IOException(e);
            }
        }
        int size = Integer.parseInt(mp.get("size"));
        encrypt = Boolean.parseBoolean(mp.get("encrypt"));

        lz4compress = Boolean.parseBoolean(mp.get("lz4compress"));
        boolean changed = false;

        Long hid = EncyptUtils.decHashArchiveName(sobj.getKey().substring(5), encrypt);
        if (this.clustered)
            mp = s3Service.getObjectMetadata(this.name, this.getClaimName(hid)).getUserMetadata();
        if (mp.containsKey("deleted")) {
            mp.remove("deleted");
            changed = true;
        }
        if (mp.containsKey("deleted-objects")) {
            mp.remove("deleted-objects");
            changed = true;
        }

        if (encrypt) {

            if (ivb != null) {
                data = EncryptUtils.decryptCBC(data, new IvParameterSpec(ivb));
            } else {
                data = EncryptUtils.decryptCBC(data);
            }
        }
        if (compress)
            data = CompressionUtils.decompressZLIB(data);
        else if (lz4compress) {
            data = CompressionUtils.decompressLz4(data, size);
        }

        String hast = new String(data);
        SDFSLogger.getLog().debug("reading hashes " + (String) mp.get("objects") + " from " + hid + " encn "
                + sobj.getKey().substring(5));
        StringTokenizer ht = new StringTokenizer(hast, ",");
        StringResult st = new StringResult();
        st.id = hid;
        st.st = ht;
        if (mp.containsKey("bsize")) {
            HashBlobArchive.currentLength.addAndGet(Integer.parseInt(mp.get("bsize")));
        }
        if (mp.containsKey("bcompressedsize")) {
            HashBlobArchive.compressedLength.addAndGet(Integer.parseInt(mp.get("bcompressedsize")));
        }
        if (changed) {
            try {
                md = sobj.getObjectMetadata();
                md.setUserMetadata(mp);
                String kn = null;
                if (this.clustered)
                    kn = this.getClaimName(hid);
                else
                    kn = sobj.getKey();

                this.updateObject(kn, md);
            } catch (Exception e) {
                throw new IOException(e);
            }
        }
        return st;
    } finally {
        if (sobj != null)
            sobj.close();
        this.s3clientLock.readLock().unlock();
    }
}

From source file:org.openflamingo.fs.s3.S3Utils.java

License:Apache License

/**
 * Bucket  ./*w  w w.ja  va  2s. c  o  m*/
 *
 * @param client     Amazon S3 Client
 * @param bucketName Bucket Name
 */
public static Map<String, String> getBucketInfo(AmazonS3Client client, String bucketName) {
    Bucket bucket = getBucket(client, bucketName);
    if (bucket == null) {
        return null;
    }

    ObjectMetadata objectMetadata = client.getObjectMetadata(bucketName, "");

    Map<String, String> map = new HashMap<String, String>();
    map.put("name", bucket.getName());
    map.put("ownerName", bucket.getOwner().getDisplayName());
    map.put("ownerId", bucket.getOwner().getId());
    setValue("create", bucket.getCreationDate(), map);
    setValue("location", client.getBucketLocation(bucketName), map);
    setValue("version", objectMetadata.getVersionId(), map);
    setValue("contentDisposition", objectMetadata.getContentDisposition(), map);
    setValue("contentType", objectMetadata.getContentType(), map);
    setValue("etag", objectMetadata.getETag(), map);
    setValue("contentEncoding", objectMetadata.getContentEncoding(), map);
    setValue("contentLength", objectMetadata.getContentLength(), map);
    setValue("lastModified", objectMetadata.getLastModified(), map);

    return map;
}

From source file:org.openflamingo.fs.s3.S3Utils.java

License:Apache License

/**
 * Object  .//from w  w w  . ja va  2 s .c  o m
 *
 * @param client     Amazon S3 Client
 * @param bucketName Bucket Name
 */
public static Map<String, String> getObject(AmazonS3Client client, String bucketName, String objectKey) {
    S3Object object = client.getObject(bucketName, objectKey);
    ObjectMetadata objectMetadata = object.getObjectMetadata();

    Map<String, String> map = new HashMap<String, String>();

    if (!object.getKey().endsWith("/")) {
        String qualifiedPath = "/" + bucketName + "/" + object.getKey();
        map.put("bucketName", object.getBucketName());
        map.put("name", FileUtils.getFilename(qualifiedPath));
        map.put("path", qualifiedPath);
    } else {
        map.put("bucketName", object.getBucketName());
        map.put("name", object.getKey());
        map.put("name", "/" + bucketName + "/" + object.getKey());
    }

    setValue("redirectionLocation", object.getRedirectLocation(), map);
    setValue("version", objectMetadata.getVersionId(), map);
    setValue("contentDisposition", objectMetadata.getContentDisposition(), map);
    setValue("contentType", objectMetadata.getContentType(), map);
    setValue("etag", objectMetadata.getETag(), map);
    setValue("contentEncoding", objectMetadata.getContentEncoding(), map);
    setValue("contentLength", objectMetadata.getContentLength(), map);
    setValue("lastModified", objectMetadata.getLastModified(), map);
    return map;
}

From source file:org.openflamingo.fs.s3.S3Utils.java

License:Apache License

/**
 * Object  ./*from  www .  j a v  a  2 s.  com*/
 *
 * @param client     Amazon S3 Client
 * @param bucketName Bucket Name
 */
public static Map<String, String> getDirectory(AmazonS3Client client, String bucketName, String objectKey) {
    S3Object object = client.getObject(bucketName, objectKey);
    ObjectMetadata objectMetadata = object.getObjectMetadata();

    List<FileInfo> filesList = new ArrayList<FileInfo>();
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(object.getBucketName())
            .withPrefix(objectKey).withDelimiter("/");

    ObjectListing objectListing = null;

    do {
        objectListing = client.listObjects(listObjectsRequest);
        List<String> commonPrefixes = objectListing.getCommonPrefixes();
        List<S3ObjectSummary> summary = objectListing.getObjectSummaries();
        listObjectsRequest.setMarker(objectListing.getNextMarker());
    } while (objectListing.isTruncated());

    Map<String, String> map = new HashMap<String, String>();

    map.put("bucketName", object.getBucketName());
    map.put("name", object.getKey());
    map.put("redirectionLocation", object.getRedirectLocation());

    setValue("version", objectMetadata.getVersionId(), map);
    setValue("contentDisposition", objectMetadata.getContentDisposition(), map);
    setValue("contentType", objectMetadata.getContentType(), map);
    setValue("etag", objectMetadata.getETag(), map);
    setValue("contentEncoding", objectMetadata.getContentEncoding(), map);
    setValue("contentLength", objectMetadata.getContentLength(), map);
    setValue("lastModified", objectMetadata.getLastModified(), map);
    return null;
}