本文整理汇总了Java中com.amazonaws.services.s3.model.S3Object.getObjectContent方法的典型用法代码示例。如果您正苦于以下问题:Java S3Object.getObjectContent方法的具体用法?Java S3Object.getObjectContent怎么用?Java S3Object.getObjectContent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.amazonaws.services.s3.model.S3Object
的用法示例。
在下文中一共展示了S3Object.getObjectContent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFileByLocationId
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
/**
* Performs a {@link GetObjectRequest} to the S3 bucket by file id for the file
*
* @param fileLocationId Id of the file to search for
* @return file found from S3
*/
@Override
public InputStream getFileByLocationId(String fileLocationId) {
final String bucketName = environment.getProperty(Constants.BUCKET_NAME_ENV_VARIABLE);
if (Strings.isNullOrEmpty(bucketName)) {
API_LOG.warn("No bucket name is specified.");
return null;
}
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, fileLocationId);
S3Object s3Object = amazonS3.getObject(getObjectRequest);
API_LOG.info("Successfully retrieved the file from S3 bucket {}", getObjectRequest.getBucketName());
return s3Object.getObjectContent();
}
示例2: readBlob
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
@Override
public InputStream readBlob(String blobName) throws IOException {
int retry = 0;
while (retry <= blobStore.numberOfRetries()) {
try {
S3Object s3Object = SocketAccess.doPrivileged(() -> blobStore.client().getObject(blobStore.bucket(), buildKey(blobName)));
return s3Object.getObjectContent();
} catch (AmazonClientException e) {
if (blobStore.shouldRetry(e) && (retry < blobStore.numberOfRetries())) {
retry++;
} else {
if (e instanceof AmazonS3Exception) {
if (404 == ((AmazonS3Exception) e).getStatusCode()) {
throw new NoSuchFileException("Blob object [" + blobName + "] not found: " + e.getMessage());
}
}
throw e;
}
}
}
throw new BlobStoreException("retries exhausted while attempting to access blob object [name:" + blobName + ", bucket:" + blobStore.bucket() + "]");
}
示例3: load
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
@Override
public boolean load(BuildCacheKey key, BuildCacheEntryReader reader) {
if (s3.doesObjectExist(bucketName, key.getHashCode())) {
logger.info("Found cache item '{}' in S3 bucket", key.getHashCode());
S3Object object = s3.getObject(bucketName, key.getHashCode());
try (InputStream is = object.getObjectContent()) {
reader.readFrom(is);
return true;
} catch (IOException e) {
throw new BuildCacheException("Error while reading cache object from S3 bucket", e);
}
} else {
logger.info("Did not find cache item '{}' in S3 bucket", key.getHashCode());
return false;
}
}
示例4: getS3Value
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
/**
* Attempt to fetch a secret from S3.
*
* @param s3path where to fetch it from
* @return the content of the file found on S3
* @throws IOException on problems streaming the content of the file
* @throws AmazonS3Exception on problems communicating with amazon
*/
private String getS3Value(final SecretPath s3path) throws IOException, AmazonS3Exception {
LOG.info("Fetching secret from s3://" + s3path.bucket + "/" + s3path.key);
if (s3Client == null) {
if (awsCredentialsProvider != null) {
s3Client = AmazonS3ClientBuilder.standard().withCredentials(awsCredentialsProvider)
.build();
} else {
s3Client = AmazonS3ClientBuilder.standard().build();
}
}
final S3Object s3object
= s3Client.getObject(new GetObjectRequest(s3path.bucket, s3path.key));
final BufferedReader reader
= new BufferedReader(new InputStreamReader(s3object.getObjectContent()));
final StringBuilder b = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
b.append(line);
}
LOG.info("Found secret");
reader.close();
return b.toString();
}
示例5: lines
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
@Override
public Stream<String> lines() {
LOGGER.debug("starting download from {}", uri);
AmazonS3URI s3URI = new AmazonS3URI(uri);
S3Object s3Object = s3Client.getObject(s3URI.getBucket(), s3URI.getKey());
InputStream stream = s3Object.getObjectContent();
return new BufferedReader(new InputStreamReader(stream)).lines();
}
示例6: storeObjectTest
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
@Test
public void storeObjectTest() throws Exception {
S3Object object = s3Client.getObject(DUMMY_BUCKET_NAME, "pref1/dummy1");
ObjectMetadata objectMetadata = object.getObjectMetadata();
BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent()));
String storedContent = reader.readLine();
reader.close();
assertEquals(objectMetadata.getContentType(), Commons.TEXT_TYPE);
assertEquals(objectMetadata.getContentLength(), KEY_CONTENT_MAPPING.get("pref1/dummy1").getBytes().length);
assertEquals(storedContent, KEY_CONTENT_MAPPING.get("pref1/dummy1"));
}
示例7: readTextFileContentFromBucket
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
/**
* Gets specified text file content from specified S3 bucket.
*
* @param bucketName
* @param fileName
* @return
*/
public String readTextFileContentFromBucket(String bucketName, String fileName) {
final S3Object s3object = s3client.getObject(bucketName, fileName);
final S3ObjectInputStream inputStream = s3object.getObjectContent();
final StringWriter writer = new StringWriter();
try {
IOUtils.copy(inputStream, writer, "UTF-8");
} catch (IOException ex) {
log.error("Error copying file from s3: " + ex);
}
return writer.toString();
}
示例8: download
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的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);
}
示例9: getPositionFromKey
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
private Position getPositionFromKey(String key) {
try {
S3Object object = s3.getObject(new GetObjectRequest(BUCKET_NAME, key));
BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent()));
StringBuilder sb = new StringBuilder();
while (true) {
String line = reader.readLine();
if (line == null) break;
sb.append(line);
}
return new Gson().fromJson(sb.toString(), Position.class);
} catch (Exception e) {
return null;
}
}
示例10: run
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的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
}
}
示例11: getStream
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
@Override
public InputStream getStream(URI uri) throws IOException {
if (client == null) {
client = clientBuilder.client(uri);
}
S3Object object = client.getObject(uri.getHost(), uri.getPath().substring(1));
if (object != null) {
return object.getObjectContent();
}
return null;
}
示例12: getString
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
public String getString(String bucket, String key) {
GetObjectRequest request = new GetObjectRequest(bucket, key);
S3Object response = client.getObject(request);
try (S3ObjectInputStream is = response.getObjectContent()) {
return CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
示例13: doInBackground
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
@Override
public File doInBackground(String... path) {
String key = path[0];
Log.d(TAG, "downloading: " + key);
S3Object object;
try {
object = client.getObject(preferences.bucketName, path[0]);
} catch (AmazonClientException ace) {
Log.e(TAG, "Error getting blob: " + key + " " + ace.getMessage());
return null;
}
long contentLength = object.getObjectMetadata().getContentLength();
dialog.setMax((int) contentLength);
File file;
try {
String eTag = object.getObjectMetadata().getETag();
if (eTag == null) {
// Some object stores do not return a sensible ETag, e.g., S3Proxy with
// filesystem backend.
eTag = UUID.randomUUID().toString();
}
file = File.createTempFile(eTag, null, MainActivity.this.getCacheDir());
byte[] buffer = new byte[4096];
try (InputStream is = object.getObjectContent();
OutputStream os = new FileOutputStream(file)) {
long progress = 0;
while (true) {
int count = is.read(buffer);
if (count == -1) {
break;
}
os.write(buffer, 0, count);
progress += count;
publishProgress((int) progress);
if (isCancelled()) {
Log.i(TAG, "Cancelling: " + key);
return null;
}
}
}
} catch (IOException ioe) {
Log.e(TAG, "Error downloading blob: " + key + " " + ioe.getMessage());
return null;
}
this.object = object;
return file;
}
示例14: readStream
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
public InputStream readStream(String bucket, String key) throws IOException {
S3Object o = getS3Object(bucket, key);
return o.getObjectContent();
}
示例15: readCompressedStream
import com.amazonaws.services.s3.model.S3Object; //导入方法依赖的package包/类
public InputStream readCompressedStream(String bucket, String key) throws IOException {
S3Object o = getS3Object(bucket, key);
return new GZIPInputStream(o.getObjectContent());
}