Package org.jets3t.service.model

Examples of org.jets3t.service.model.StorageObject


        RestStorageService service = getStorageService(getCredentials());
        StorageBucket bucket = createBucketForTest("testIsObjecInBucket");
        String bucketName = bucket.getName();

        try {
            service.putObject(bucketName, new StorageObject("does-exist"));

            assertTrue(service.isObjectInBucket(bucketName, "does-exist"));

            assertFalse(service.isObjectInBucket(bucketName, "does-not-exist"));
        } finally {
View Full Code Here


            assertEquals(0, getObjectHeadsEventCount[0]);
            assertEquals(0, getObjectsList.size());
            assertEquals(0, deleteObjectsEventCount[0]);

            StorageObject[] objects = new StorageObject[] {
                new StorageObject("one.txt", "Some data"),
                new StorageObject("twö.txt", "Some data"),
                new StorageObject("thréè.txt", "Some data"),
                new StorageObject("fôür.txt", "Some data"),
                new StorageObject("fîvæ∫.txt", "Some data")
            };

            // Upload multiple objects
            boolean success = threadedService.putObjects(bucketName, objects);
            assertTrue(success);
View Full Code Here

        try {
            SimpleThreadedStorageService simpleThreadedService =
                new SimpleThreadedStorageService(service);

            StorageObject[] objects = new StorageObject[] {
                new StorageObject("1-one.txt", "Some data"),
                new StorageObject("2-twö.txt", "Some data"),
                new StorageObject("3-thréè.txt", "Some data"),
                new StorageObject("4-fôür.txt", "Some data"),
                new StorageObject("5-fîvæ∫.txt", "Some data")
            };

            // Upload multiple objects
            StorageObject[] putObjects =
                simpleThreadedService.putObjects(bucketName, objects);
View Full Code Here

            local1FOS.close();
            // Ensure local file's timestamp differs by at least 1 sec
            local1.setLastModified(local1.lastModified() + 1000);

            data = "Updated remote file".getBytes("UTF-8");
            StorageObject remoteObject = new StorageObject(local3Path);
            // Ensure remote file's JetS3t timestamp differs from local file by at least 1 sec
            remoteObject.addMetadata(Constants.METADATA_JETS3T_LOCAL_FILE_DATE,
                ServiceUtils.formatIso8601Date(new Date(local3.lastModified() + 1000)));
            remoteObject.setDataInputStream(new ByteArrayInputStream(data));
            remoteObject.setContentLength(data.length);
            service.putObject(bucketName, remoteObject);

            objectMap = comparer.buildObjectMap(
                service, bucket.getName(), "", objectKeyToFilepathMap, false, false, null, null);

            comparerResults =
                comparer.buildDiscrepancyLists(objectKeyToFilepathMap, objectMap);
            assertEquals(3, comparerResults.alreadySynchronisedKeys.size());
            assertEquals(0, comparerResults.onlyOnClientKeys.size());
            assertEquals(0, comparerResults.onlyOnServerKeys.size());
            assertEquals(1, comparerResults.updatedOnClientKeys.size());
            assertTrue(comparerResults.updatedOnClientKeys.contains(local1Path));
            assertEquals(1, comparerResults.updatedOnServerKeys.size());
            assertTrue(comparerResults.updatedOnServerKeys.contains(local3Path));

            // Create new local and remote objects, then confirm discrepancies
            File local4 = File.createTempFile("four", ".txt", parentDir2);
            String local4Path = parentDir2.getName() + File.separator + local4.getName();
            remoteObject = new StorageObject("new-on-service.txt");
            service.putObject(bucketName, remoteObject);

            objectKeyToFilepathMap = comparer.buildObjectKeyToFilepathMap(
                new File[] {parentDir1, parentDir2}, "", true);
            objectMap = comparer.buildObjectMap(
View Full Code Here

    public StorageObject[] getObjects(String bucketName, final String[] objectKeys)
        throws ServiceException
    {
        StorageObject[] objects = new StorageObject[objectKeys.length];
        for (int i = 0; i < objectKeys.length; i++) {
            objects[i] = new StorageObject(objectKeys[i]);
        }
        return getObjects(bucketName, objects);
    }
View Full Code Here

        }

        public void run() {
            BufferedInputStream bufferedInputStream = null;
            BufferedOutputStream bufferedOutputStream = null;
            StorageObject object = null;

            try {
                object = storageService.getObject(
                    bucketName, objectKey);

                // Replace the object in the download package with the downloaded version to make metadata available.
                downloadPackage.setObject(object);

                // Setup monitoring of stream bytes transferred.
                interruptableInputStream = new InterruptableInputStream(object.getDataInputStream());
                bufferedInputStream = new BufferedInputStream(
                    new ProgressMonitoredInputStream(interruptableInputStream, progressMonitor));

                bufferedOutputStream = new BufferedOutputStream(
                    downloadPackage.getOutputStream());

                MessageDigest messageDigest = null;
                try {
                    messageDigest = MessageDigest.getInstance("MD5");
                } catch (NoSuchAlgorithmException e) {
                    if (log.isWarnEnabled()) {
                        log.warn("Unable to calculate MD5 hash of data received as algorithm is not available", e);
                    }
                }

                try {
                    byte[] buffer = new byte[1024];
                    int byteCount = -1;

                    while ((byteCount = bufferedInputStream.read(buffer)) != -1) {
                        bufferedOutputStream.write(buffer, 0, byteCount);

                        if (messageDigest != null) {
                            messageDigest.update(buffer, 0, byteCount);
                        }
                    }

                    // Check that actual bytes received match expected hash value
                    if (messageDigest != null) {
                        byte[] dataMD5Hash = messageDigest.digest();
                        String hexMD5OfDownloadedData = ServiceUtils.toHex(dataMD5Hash);

                        // Don't check MD5 hash against ETag if ETag doesn't look like an MD5 value
                        if (!ServiceUtils.isEtagAlsoAnMD5Hash(object.getETag()))
                        {
                            // Use JetS3t's own MD5 hash metadata value for comparison, if it's available
                            if (!hexMD5OfDownloadedData.equals(object.getMd5HashAsHex())) {
                                if (log.isWarnEnabled()) {
                                    log.warn("Unable to verify MD5 hash of downloaded data against"
                                        + " ETag returned by service because ETag value \""
                                        + object.getETag() + "\" is not an MD5 hash value"
                                        + ", for object key: " + object.getKey());
                                }
                            }
                        } else {
                            if (!hexMD5OfDownloadedData.equals(object.getETag())) {
                                throw new ServiceException("Mismatch between MD5 hash of downloaded data ("
                                    + hexMD5OfDownloadedData + ") and ETag returned by service ("
                                    + object.getETag() + ") for object key: "
                                    + object.getKey());
                            } else {
                                if (log.isDebugEnabled()) {
                                    log.debug("Object download was automatically verified, the calculated MD5 hash "+
                                        "value matched the ETag provided by service: " + object.getKey());
                                }
                            }
                        }
                    }

                } finally {
                    if (bufferedOutputStream != null) {
                        bufferedOutputStream.close();
                    }
                    if (bufferedInputStream != null) {
                        bufferedInputStream.close();
                    }
                }

                object.setDataInputStream(null);
                object.setDataInputFile(downloadPackage.getDataFile());

                // If data was downloaded to a file, set the file's Last Modified date
                // to the original last modified date metadata stored with the object.
                if (restoreLastModifiedDate && downloadPackage.getDataFile() != null) {
                    String metadataLocalFileDate = (String) object.getMetadata(
                        Constants.METADATA_JETS3T_LOCAL_FILE_DATE);

                    if (metadataLocalFileDate != null) {
                        if (log.isDebugEnabled()) {
                            log.debug("Restoring original Last Modified date for object '"
                                + object.getKey() + "' to file '" + downloadPackage.getDataFile()
                                + "': " + metadataLocalFileDate);
                        }
                        downloadPackage.getDataFile().setLastModified(
                            ServiceUtils.parseIso8601Date(metadataLocalFileDate).getTime());
                    }
View Full Code Here

        // Check files on server against local client files.
        Iterator<Map.Entry<String, StorageObject>> objectsMapIter = objectsMap.entrySet().iterator();
        while (objectsMapIter.hasNext()) {
            Map.Entry<String, StorageObject> entry = objectsMapIter.next();
            String keyPath = entry.getKey();
            StorageObject storageObject = entry.getValue();

            String[] splitPathComponents = splitFilePathIntoDirPaths(
                keyPath, storageObject.isDirectoryPlaceholder());

            int componentCount = 0;
            for (String localPath: splitPathComponents) {
                componentCount += 1;

                String filepath = objectKeyToFilepathMap.get(localPath);

                // Check whether local file is already on server
                if (filepath != null) {
                    // File has been backed up in the past, is it still up-to-date?
                    File file = new File(filepath);

                    // We don't care about directory date changes, as long as it's present.
                    if (file.isDirectory()) {
                        // Only flag key path as already synced if the current localPath
                        // is also equivalent to the *full* path of the object in the storage
                        // service, not just an object's parent directory. (Issue #69)
                        if (componentCount == splitPathComponents.length) {
                            alreadySynchronisedKeys.add(keyPath);
                            alreadySynchronisedLocalPaths.add(localPath);
                            boolean wasRemoved = onlyOnClientKeys.remove(keyPath);

                            // Backwards-compatibility with JetS3t directory place-holders
                            // without trailing slash (/) suffixes
                            if (!wasRemoved && !keyPath.endsWith("/")
                                && storageObject.isDirectoryPlaceholder())
                            {
                                onlyOnClientKeys.remove(keyPath + "/");
                            }
                        }
                    }
                    // If upload is forced, don't bother comparing object MD5 hashes
                    else if (isForceUpload) {
                        // Treat file as if it's already synchronized with the service, whether
                        // it is or not doesn't really matter since we're uploading it regardless
                        alreadySynchronisedKeys.add(keyPath);
                        alreadySynchronisedLocalPaths.add(localPath);
                        onlyOnClientKeys.remove(keyPath);
                    }
                    // Compare file hashes.
                    else {
                        String fileHashAsBase64 = ServiceUtils.toBase64(
                            generateFileMD5Hash(file, storageObject.getKey(), progressWatcher));

                        // Get the service object's Base64 hash.
                        String objectHash = null;
                        if (storageObject.containsMetadata(StorageObject.METADATA_HEADER_ORIGINAL_HASH_MD5)) {
                            // Use the object's *original* hash, as it is an encoded version of a local file.
                            objectHash = (String) storageObject.getMetadata(
                                StorageObject.METADATA_HEADER_ORIGINAL_HASH_MD5);
                            if (log.isDebugEnabled()) {
                                log.debug("Object in service is encoded, using the object's original hash value for: "
                                + storageObject.getKey());
                            }
                        } else {
                            // The object wasn't altered when uploaded, so use its current hash.
                            objectHash = storageObject.getMd5HashAsBase64();
                        }

                        if (fileHashAsBase64.equals(objectHash)) {
                            // Hashes match so file is already synchronised.
                            alreadySynchronisedKeys.add(keyPath);
                            alreadySynchronisedLocalPaths.add(localPath);
                            onlyOnClientKeys.remove(keyPath);
                        } else {
                            // File is out-of-synch. Check which version has the latest date.
                            Date objectLastModified = null;
                            String metadataLocalFileDate = (String) storageObject.getMetadata(
                                Constants.METADATA_JETS3T_LOCAL_FILE_DATE);

                            if (metadataLocalFileDate == null) {
                                // This is risky as local file times and service times don't match!
                                if (!isAssumeLocalLatestInMismatch() && log.isWarnEnabled()) {
                                    log.warn("Using service last modified date as file date. This is not reliable "
                                    + "as the time according to service can differ from your local system time. "
                                    + "Please use the metadata item "
                                    + Constants.METADATA_JETS3T_LOCAL_FILE_DATE);
                                }
                                objectLastModified = storageObject.getLastModifiedDate();
                            } else {
                                objectLastModified = ServiceUtils
                                    .parseIso8601Date(metadataLocalFileDate);
                            }
                            if (objectLastModified.getTime() > file.lastModified()) {
                                updatedOnServerKeys.add(keyPath);
                                onlyOnClientKeys.remove(keyPath);
                            } else if (objectLastModified.getTime() < file.lastModified()) {
                                updatedOnClientKeys.add(keyPath);
                                onlyOnClientKeys.remove(keyPath);
                            } else {
                                // Local file date and service object date values match exactly, yet the
                                // local file has a different hash. This shouldn't ever happen, but
                                // sometimes does with Excel files.
                                if (isAssumeLocalLatestInMismatch()) {
                                    if (log.isWarnEnabled()) {
                                        log.warn("Backed-up object \"" + storageObject.getKey()
                                        + "\" and local file \"" + file.getName()
                                        + "\" have the same date but different hash values. "
                                        + "Assuming local file is the latest version.");
                                    }
                                    updatedOnClientKeys.add(keyPath);
                                    onlyOnClientKeys.remove(keyPath);
                                } else {
                                    throw new IOException("Backed-up object \"" + storageObject.getKey()
                                        + "\" and local file \"" + file.getName()
                                        + "\" have the same date but different hash values. "
                                        + "This shouldn't happen!");
                                }

View Full Code Here

            // Build upload and part lists from new multipart uploads, where new
            // MultipartUpload objects were captured by this method's
            // captureMultipartUploadObjectsEventAdaptor)
            for (MultipartUpload upload: multipartUploadList) {
                StorageObject object = objectsByKey.get(upload.getObjectKey());
                if (object.getDataInputFile() == null) {
                    throw new ServiceException();
                }
                partObjects = splitFileIntoObjectsByMaxPartSize(
                    upload.getObjectKey(),
                    object.getDataInputFile());
                uploadAndPartsList.add(
                    new MultipartUploadAndParts(upload, partObjects));
            }

            // Upload all parts for all multipart uploads
View Full Code Here

            if (cloudfrontEnabled) {
                String encodedUrlPath = RestUtils.encodeUrlPath(appFile.getRelativePath(), "/");

                //Determine when the object was last modified in S3 and append the timestamp to the URL to force cloudfront cache busting
                StorageObject storageObject = getS3Service(storageConfiguration).getObjectDetails(storageConfiguration.getBucketName(), appFile.getRelativePath());
                if (storageObject != null && storageObject.getLastModifiedDate() != null) {
                    encodedUrlPath += String.format("?%s", TimeUnit.MILLISECONDS.toSeconds(storageObject.getLastModifiedDate().getTime()));
                }

                return CloudFrontService.signUrlCanned(String.format("%s/%s", cloudfrontURL, encodedUrlPath), cloudfrontKeyPairID, cloudfrontPrivateKey, expiryDate);
            } else {
                return getS3Service(storageConfiguration).createSignedGetUrl(storageConfiguration.getBucketName(), appFile.getRelativePath(), expiryDate, false);
View Full Code Here

    long length = 0;

    if (s3Artifact.getFilesize().isPresent()) {
      length = s3Artifact.getFilesize().get();
    } else {
      StorageObject details = s3.getObjectDetails(s3Artifact.getS3Bucket(), s3Artifact.getS3ObjectKey());

      Preconditions.checkNotNull(details, "Couldn't find object at %s/%s", s3Artifact.getS3Bucket(), s3Artifact.getS3ObjectKey());

      length = details.getContentLength();
    }

    int numChunks = (int) (length / configuration.getS3ChunkSize());

    if (length % configuration.getS3ChunkSize() > 0) {
View Full Code Here

TOP

Related Classes of org.jets3t.service.model.StorageObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.