当前位置: 首页>>代码示例>>Java>>正文


Java AmazonS3.getObject方法代码示例

本文整理汇总了Java中com.amazonaws.services.s3.AmazonS3.getObject方法的典型用法代码示例。如果您正苦于以下问题:Java AmazonS3.getObject方法的具体用法?Java AmazonS3.getObject怎么用?Java AmazonS3.getObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.amazonaws.services.s3.AmazonS3的用法示例。


在下文中一共展示了AmazonS3.getObject方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: get

import com.amazonaws.services.s3.AmazonS3; //导入方法依赖的package包/类
public Object get(Object[] params) {
    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
    try {
        S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, params[0].toString()));
        InputStream objectData = object.getObjectContent();
        byte[] bytes = IOUtils.toByteArray(is);
        ByteBuffer b = ByteBuffer.wrap(bytes);
        return b;
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which " +
            "means your request made it " +
            "to Amazon S3, but was rejected with an error response" +
            " for some reason.");
        System.out.println("Error Message:" + ase.getMessage());
        System.out.println("HTTP Status Code:" + ase.getStatusCode());
        System.out.println("AWS Error Code:" + ase.getErrorCode());
        System.out.println("Error Type:" + ase.getErrorType());
        System.out.println("Request ID:" + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which " +
            "means the client encountered " +
            "an internal error while trying to " +
            "communicate with S3, " +
            "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }

}
 
开发者ID:EventHorizon27,项目名称:dataset-lib,代码行数:29,代码来源:S3Provider.java

示例2: getReaderFromObjectInfo

import com.amazonaws.services.s3.AmazonS3; //导入方法依赖的package包/类
public static BufferedReader getReaderFromObjectInfo(ObjectInfoSimple objectInfo) {
    AmazonS3 s3Client = AmazonS3Provider.getS3Client();

    S3Object object = s3Client.getObject(objectInfo.getBucket(), objectInfo.getKey());
    S3ObjectInputStream objectContentRawStream = object.getObjectContent();

    return new BufferedReader(new InputStreamReader(objectContentRawStream));
}
 
开发者ID:d2si-oss,项目名称:ooso,代码行数:9,代码来源:Commons.java

示例3: download

import com.amazonaws.services.s3.AmazonS3; //导入方法依赖的package包/类
/**
 * Download an object data as a file
 *
 * @param remoteObjectName the name of object/key which contents should be downloaded
 * @param localFileName the location and file name on the local machine, where the file will be downloaded
 * @throws S3OperationException if there is an error during data transfer
 */
@PublicAtsApi
public void download( String remoteObjectName, String localFileName ) throws S3OperationException,
                                                                      IllegalArgumentException {

    AmazonS3 s3Client = getClient();

    localFileName = IoUtils.normalizeFilePath(localFileName);
    String localDirName = IoUtils.getFilePath(localFileName);
    String localFileOnlyName = IoUtils.getFileName(localFileName);
    File localDir = new File(localDirName);
    if (localDir.exists()) {
        if (localDir.isFile()) {
            throw new IllegalArgumentException("Could not create file " + localFileOnlyName + " into existing file "
                                               + localDirName);
        }
        // else dir exists
    } else {
        LOG.debug("Creating target directory path " + localDirName);
        if (!localDir.mkdirs()) {
            throw new S3OperationException("Could not create local directory path '" + localDirName
                                           + "' for local file specified '" + localFileName + "'");
        }
    }

    S3Object obj = s3Client.getObject(bucketName, remoteObjectName);
    try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(localFileName)));
            S3ObjectInputStream s3is = obj.getObjectContent();) {
        byte[] readBuffArr = new byte[4096];
        int readBytes = 0;
        while ( (readBytes = s3is.read(readBuffArr)) >= 0) {
            bos.write(readBuffArr, 0, readBytes);
        }
    } catch (Exception e) {
        handleExeption(e, "Error while downloading object " + remoteObjectName + " to local file " + localFileName
                          + ". If error persists check your endpoint, credentials and permissions.");
    }
    LOG.info("S3 object '" + remoteObjectName + "; is downloaded successfully from bucket '" + bucketName
             + "' to file " + localFileName);
}
 
开发者ID:Axway,项目名称:ats-framework,代码行数:47,代码来源:S3Operations.java

示例4: run

import com.amazonaws.services.s3.AmazonS3; //导入方法依赖的package包/类
@Override
public void run() {
    super.run();

    String awsCredentialsProfile = this.readStringArgument("awsProfile", "default");
    String bucket = this.readStringArgument("bucket");
    String objectKey = this.readStringArgument("objectKey");
    String targetFilePath = this.readStringArgument("targetFile");
    Boolean overwrite = this.readBooleanArgument("overwrite", false);

    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider(awsCredentialsProfile));
    S3Object object = s3Client.getObject(
            new GetObjectRequest(bucket, objectKey));
    InputStream objectDataStream = object.getObjectContent();

    if (targetFilePath != null) {
        File targetFile = new File(targetFilePath);
        if (!targetFile.isAbsolute()) {
            targetFile = Paths.get(this.getActor().getTempDir().getAbsolutePath(), targetFilePath).toFile();
        }
        targetFile.getParentFile().mkdirs();

        try {
            if (overwrite) {
                Files.copy(objectDataStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            } else {
                Files.copy(objectDataStream, targetFile.toPath());
            }
        } catch (Exception ex) {
            throw new RuntimeException(String.format(
                    "Failed to transfer data from the input stream into file %s",
                    targetFilePath), ex);
        }
        
        this.writeArgument("targetFile", targetFile.getAbsolutePath());
    } else {
        // TODO: Make targetFile arg optional so this branch can execute.
        // Read data in memory and write it to an output value
    }
}
 
开发者ID:mcdcorp,项目名称:opentest,代码行数:41,代码来源:GetS3Object.java

示例5: SecureShellAuthentication

import com.amazonaws.services.s3.AmazonS3; //导入方法依赖的package包/类
public SecureShellAuthentication(Bucket bucket, AmazonS3 client) {
    factory = new JschConfigSessionFactory() {

        @Override
        public synchronized RemoteSession getSession(URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms) throws TransportException {
            // Do not check for default ssh user config
            fs.setUserHome(null);
            return super.getSession(uri, credentialsProvider, fs, tms);
        }

        @Override
        protected void configure(OpenSshConfig.Host host, Session session) {
            session.setConfig("HashKnownHosts", "no");
            if ("localhost".equalsIgnoreCase(host.getHostName())) {
                session.setConfig("StrictHostKeyChecking", "no");
            }
        }

        @Override
        protected void configureJSch(JSch jsch) {
            S3Object file;
            file = client.getObject(bucket.getName(), ".ssh/known_hosts");
            try (InputStream is = file.getObjectContent()) {
                jsch.setKnownHosts(is);
            } catch (IOException | JSchException e) {
                throw new IllegalArgumentException("Missing known hosts file on s3: .ssh/known_hosts", e);
            }
            file = client.getObject(bucket.getName(), ".ssh/id_rsa");
            try (InputStream is = file.getObjectContent()) {
                jsch.addIdentity("git", IOUtils.toByteArray(is), null, new byte[0]);
            } catch (IOException | JSchException e) {
                throw new IllegalArgumentException("Missing key file on s3: .ssh/id_rsa", e);
            }
        }
    };
}
 
开发者ID:berlam,项目名称:github-bucket,代码行数:37,代码来源:SecureShellAuthentication.java


注:本文中的com.amazonaws.services.s3.AmazonS3.getObject方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。