Search in sources :

Example 1 with GSObject

use of org.jets3t.service.model.GSObject in project tdi-studio-se by Talend.

the class GSObjectUtil method genObjectByFileMap.

public List<GSObject> genObjectByFileMap(java.util.Map<String, String> fileMap) throws Exception, IOException {
    Set<String> localFiles = fileMap.keySet();
    List<GSObject> objects = new ArrayList<GSObject>();
    for (String localFilePath : localFiles) {
        GSObject object = new GSObject(new File(localFilePath));
        object.setKey(fileMap.get(localFilePath));
        objects.add(object);
    }
    return objects;
}
Also used : GSObject(org.jets3t.service.model.GSObject) ArrayList(java.util.ArrayList) File(java.io.File)

Example 2 with GSObject

use of org.jets3t.service.model.GSObject in project alluxio by Alluxio.

the class GCSUnderFileSystem method createEmptyObject.

@Override
public boolean createEmptyObject(String key) {
    try {
        GSObject obj = new GSObject(key);
        obj.setDataInputStream(new ByteArrayInputStream(new byte[0]));
        obj.setContentLength(0);
        obj.setMd5Hash(DIR_HASH);
        obj.setContentType(Mimetypes.MIMETYPE_BINARY_OCTET_STREAM);
        mClient.putObject(mBucketName, obj);
        return true;
    } catch (ServiceException e) {
        LOG.error("Failed to create directory: {}", key, e);
        return false;
    }
}
Also used : ServiceException(org.jets3t.service.ServiceException) GSObject(org.jets3t.service.model.GSObject) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 3 with GSObject

use of org.jets3t.service.model.GSObject in project tdi-studio-se by Talend.

the class GSObjectUtil method genGSObjectList.

public List<GSObject> genGSObjectList(List<GSObject> objects, File file, String keyParent, boolean isGenFileObject, boolean isGenFolderObject) throws NoSuchAlgorithmException, IOException {
    if (file.isDirectory()) {
        if (keyParent != null && !"".equals(keyParent)) {
            if (keyParent.trim().lastIndexOf("/") != keyParent.trim().length() - 1) {
                keyParent = keyParent + "/";
            }
            if (isGenFolderObject) {
                objects.add(new GSObject(keyParent));
            }
        }
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isDirectory()) {
                objects = genGSObjectList(objects, f, keyParent + f.getName() + "/", isGenFileObject, isGenFolderObject);
            } else {
                objects = genGSObjectList(objects, f, keyParent + f.getName(), isGenFileObject, isGenFolderObject);
            }
        }
    } else {
        if (isGenFileObject) {
            GSObject obj = new GSObject(file);
            obj.setKey(keyParent);
            objects.add(obj);
        }
    }
    return objects;
}
Also used : GSObject(org.jets3t.service.model.GSObject) File(java.io.File)

Example 4 with GSObject

use of org.jets3t.service.model.GSObject in project alluxio by Alluxio.

the class GCSInputStream method openStream.

/**
 * Opens a new stream at mPos if the wrapped stream mInputStream is null.
 */
private void openStream() throws IOException {
    ServiceException lastException = null;
    String errorMessage = String.format("Failed to open key: %s bucket: %s", mKey, mBucketName);
    while (mRetryPolicy.attempt()) {
        try {
            GSObject object;
            if (mPos > 0) {
                object = mClient.getObject(mBucketName, mKey, null, null, null, null, mPos, null);
            } else {
                object = mClient.getObject(mBucketName, mKey);
            }
            mInputStream = new BufferedInputStream(object.getDataInputStream());
            return;
        } catch (ServiceException e) {
            errorMessage = String.format("Failed to open key: %s bucket: %s attempts: %d error: %s", mKey, mBucketName, mRetryPolicy.getAttemptCount(), e.getMessage());
            if (e.getResponseCode() != HttpStatus.SC_NOT_FOUND) {
                throw new IOException(errorMessage, e);
            }
            // Key does not exist
            lastException = e;
        }
    }
    // Failed after retrying key does not exist
    throw new IOException(errorMessage, lastException);
}
Also used : ServiceException(org.jets3t.service.ServiceException) GSObject(org.jets3t.service.model.GSObject) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException)

Example 5 with GSObject

use of org.jets3t.service.model.GSObject in project alluxio by Alluxio.

the class GCSUnderFileSystem method copyObject.

@Override
protected boolean copyObject(String src, String dst) {
    LOG.debug("Copying {} to {}", src, dst);
    GSObject obj = new GSObject(dst);
    // Retry copy for a few times, in case some Jets3t or GCS internal errors happened during copy.
    int retries = 3;
    for (int i = 0; i < retries; i++) {
        try {
            mClient.copyObject(mBucketName, src, mBucketName, obj, false);
            return true;
        } catch (ServiceException e) {
            LOG.error("Failed to copy file {} to {}", src, dst, e);
            if (i != retries - 1) {
                LOG.error("Retrying copying file {} to {}", src, dst);
            }
        }
    }
    LOG.error("Failed to copy file {} to {}, after {} retries", src, dst, retries);
    return false;
}
Also used : ServiceException(org.jets3t.service.ServiceException) GSObject(org.jets3t.service.model.GSObject)

Aggregations

GSObject (org.jets3t.service.model.GSObject)6 ServiceException (org.jets3t.service.ServiceException)4 File (java.io.File)2 IOException (java.io.IOException)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1