本文整理匯總了Java中com.amazonaws.services.s3.model.ObjectMetadata.setContentLength方法的典型用法代碼示例。如果您正苦於以下問題:Java ObjectMetadata.setContentLength方法的具體用法?Java ObjectMetadata.setContentLength怎麽用?Java ObjectMetadata.setContentLength使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.amazonaws.services.s3.model.ObjectMetadata
的用法示例。
在下文中一共展示了ObjectMetadata.setContentLength方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: store
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
/**
* Stores the {@link InputStream} as an object in the S3 bucket.
*
* @param keyName The requested key name for the object.
* @param inStream The {@link InputStream} to write out to an object in S3.
* @param size The size of the {@link InputStream}.
* @return A {@link CompletableFuture} that will eventually contain the S3 object key.
*/
@Override
public CompletableFuture<String> store(String keyName, InputStream inStream, long size) {
final String bucketName = environment.getProperty(Constants.BUCKET_NAME_ENV_VARIABLE);
final String kmsKey = environment.getProperty(Constants.KMS_KEY_ENV_VARIABLE);
if (Strings.isNullOrEmpty(bucketName) || Strings.isNullOrEmpty(kmsKey)) {
API_LOG.warn("No bucket name is specified or no KMS key specified.");
return CompletableFuture.completedFuture("");
}
ObjectMetadata s3ObjectMetadata = new ObjectMetadata();
s3ObjectMetadata.setContentLength(size);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inStream, s3ObjectMetadata)
.withSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(kmsKey));
API_LOG.info("Writing object {} to S3 bucket {}", keyName, bucketName);
return actOnItem(putObjectRequest);
}
示例2: createShortUrl
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
/**
* The method creates a short URL code for the given url
*
* @param url
* @return
*/
public ShortUrl createShortUrl(String url, String code) {
logger.info("storing the url {} in the bucket {}", url, this.bucket);
// Create the link for the short code
if (code == null) {
code = getObjectCode();
}
String loadFile = redirectFile.replace("REPLACE", url);
byte[] fileContentBytes = loadFile.getBytes(StandardCharsets.UTF_8);
InputStream fileInputStream = new ByteArrayInputStream(fileContentBytes);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/html");
metadata.addUserMetadata("url", url);
metadata.setContentLength(fileContentBytes.length);
PutObjectRequest putObjectRequest = new PutObjectRequest(this.bucket, code, fileInputStream, metadata)
.withCannedAcl(CannedAccessControlList.PublicRead);
this.s3Client.putObject(putObjectRequest);
createDummyRecord(url, code);
return new ShortUrl(url, code);
}
示例3: shouldNotUploadStreamingWithWrongEncryptionKey
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
/**
* Tests if Object can be uploaded with wrong KMS Key
*/
@Test
public void shouldNotUploadStreamingWithWrongEncryptionKey() {
final byte[] bytes = UPLOAD_FILE_NAME.getBytes();
final InputStream stream = new ByteArrayInputStream(bytes);
final String objectKey = UUID.randomUUID().toString();
s3Client.createBucket(BUCKET_NAME);
final ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(bytes.length);
final PutObjectRequest putObjectRequest =
new PutObjectRequest(BUCKET_NAME, objectKey, stream, metadata);
putObjectRequest.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(TEST_WRONG_KEYREF));
thrown.expect(AmazonS3Exception.class);
thrown.expectMessage(containsString("Status Code: 400; Error Code: KMS.NotFoundException"));
s3Client.putObject(putObjectRequest);
}
示例4: createEmptyObject
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
private void createEmptyObject(final String bucketName, final String objectName)
throws AmazonClientException, AmazonServiceException {
final InputStream im = new InputStream() {
@Override
public int read() throws IOException {
return -1;
}
};
final ObjectMetadata om = new ObjectMetadata();
om.setContentLength(0L);
if (StringUtils.isNotBlank(serverSideEncryptionAlgorithm)) {
om.setServerSideEncryption(serverSideEncryptionAlgorithm);
}
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, im, om);
putObjectRequest.setCannedAcl(cannedACL);
s3.putObject(putObjectRequest);
statistics.incrementWriteOps(1);
}
示例5: putObject
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
@Override
public PutObjectResult putObject(String bucketName, String key, String content)
throws AmazonServiceException, SdkClientException {
rejectNull(bucketName, "Bucket name must be provided");
rejectNull(key, "Object key must be provided");
rejectNull(content, "String content must be provided");
byte[] contentBytes = content.getBytes(StringUtils.UTF8);
InputStream is = new ByteArrayInputStream(contentBytes);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/plain");
metadata.setContentLength(contentBytes.length);
return putObject(new PutObjectRequest(bucketName, key, is, metadata));
}
示例6: put
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
public void put(InputStream inputStream, Long contentLength, URI destination) {
checkRequiredJigsawModuleIsOnPath();
try {
S3RegionalResource s3RegionalResource = new S3RegionalResource(destination);
String bucketName = s3RegionalResource.getBucketName();
String s3BucketKey = s3RegionalResource.getKey();
configureClient(s3RegionalResource);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(contentLength);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, s3BucketKey, inputStream, objectMetadata);
LOGGER.debug("Attempting to put resource:[{}] into s3 bucket [{}]", s3BucketKey, bucketName);
amazonS3Client.putObject(putObjectRequest);
} catch (AmazonClientException e) {
throw ResourceExceptions.putFailed(destination, e);
}
}
示例7: writeCsvFile
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
/**
* Write a new inventory report to S3 and returns a locator which includes this inventory report's information
* @return Locator which includes the information of this new report
* @throws IOException thrown when GZIPOutputStream not created successfully or csvMapper.write() fails
*/
public InventoryManifest.Locator writeCsvFile(List<InventoryReportLine> inventoryReportLine) throws IOException{
CsvMapper csvMapper = new CsvMapper();
csvMapper.enable(JsonGenerator.Feature.IGNORE_UNKNOWN);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
csvMapper.writer(schema).writeValues(gzipOutputStream).writeAll(inventoryReportLine).close();
byte[] zipByteArray = byteArrayOutputStream.toByteArray();
InputStream zipInputStream = new ByteArrayInputStream(zipByteArray);
ObjectMetadata metaData = new ObjectMetadata();
metaData.setContentLength(zipByteArray.length);
PutObjectRequest request = new PutObjectRequest(bucketName, outputInventoryReportKey, zipInputStream, metaData);
s3Client.putObject(request);
return this.buildLocator(zipByteArray);
}
示例8: describeUpload
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
private S3UploadDescriptor describeUpload(FileStatus sourceFileStatus, Path targetPath) throws IOException {
URI targetUri = targetPath.toUri();
String bucketName = PathUtil.toBucketName(targetUri);
String key = PathUtil.toBucketKey(targetUri);
Path sourcePath = sourceFileStatus.getPath();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(sourceFileStatus.getLen());
if (conf.getBoolean(ConfigurationVariable.S3_SERVER_SIDE_ENCRYPTION)) {
metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
}
return new S3UploadDescriptor(sourcePath, bucketName, key, metadata);
}
示例9: doInBackground
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
@Override
public Long doInBackground(Void... unused) {
Log.d(TAG, "Upload Blob Task:");
MainActivity.YassPreferences preferences = new MainActivity.YassPreferences(context);
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
if (!isConnected) {
Log.d(TAG, "Skipping camera upload because network is disconnected");
return null;
}
if (preferences.cameraUploadOnlyOnWifi && activeNetwork.getType() != ConnectivityManager.TYPE_WIFI) {
Log.d(TAG, "Skipping camera upload because Wifi is disconnected");
return null;
}
long serial;
Uri uri;
String fileName;
long fileSize;
SQLiteDatabase db = new YassDbHelper(context).getReadableDatabase();
try {
String[] projection = {
"serial",
"file_uri",
"file_name",
"file_size"
};
String selection = null;
String[] selectionArgs = null;
String groupBy = null;
String having = null;
String orderBy = "serial ASC";
String limit = "1";
Cursor cursor = db.query("camera_uploads", projection, selection, selectionArgs,
groupBy, having, orderBy, limit);
try {
if (!cursor.moveToNext()) {
Log.d(TAG, "Did not find image to upload");
return null;
}
serial = cursor.getLong(cursor.getColumnIndexOrThrow("serial"));
uri = Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow("file_uri")));
fileName = cursor.getString(cursor.getColumnIndexOrThrow("file_name"));
fileSize = cursor.getLong(cursor.getColumnIndexOrThrow("file_size"));
} finally {
cursor.close();
}
} finally {
db.close();
}
Log.d(TAG, "Found image to upload: " + fileName);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(fileSize);
metadata.setContentType(context.getContentResolver().getType(uri));
PutObjectResult result;
try (InputStream is = context.getContentResolver().openInputStream(uri)) {
// TODO: limited to 5 GB
result = MainActivity.getS3Client(preferences).putObject(preferences.bucketName,
"Camera Uploads/" + fileName, is, metadata);
} catch (AmazonClientException | IOException e) {
Log.e(TAG, "Could not upload file: " + e.getMessage());
return null;
}
return serial;
}
示例10: sendToQueueUsingS3
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
public static String sendToQueueUsingS3(AmazonSQS sqs, String queueUrl, AmazonS3 s3, String bucketName,
Map<String, String> headers, byte[] message, Callable<String> s3IdFactory) {
Preconditions.checkNotNull(sqs);
Preconditions.checkNotNull(s3);
Preconditions.checkNotNull(queueUrl);
Preconditions.checkNotNull(bucketName);
Preconditions.checkNotNull(message);
String s3Id;
try {
s3Id = s3IdFactory.call();
} catch (final Exception e1) {
throw new RuntimeException(e1);
}
final ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(message.length);
for (final Entry<String, String> header : headers.entrySet()) {
metadata.setHeader(header.getKey(), header.getValue());
}
s3.putObject(bucketName, s3Id, new ByteArrayInputStream(message), metadata);
try {
sqs.sendMessage(queueUrl, s3Id);
} catch (final RuntimeException e) {
try {
s3.deleteObject(bucketName, s3Id);
throw e;
} catch (final RuntimeException e2) {
throw new io.reactivex.exceptions.CompositeException(e, e2);
}
}
return s3Id;
}
示例11: createDummyRecord
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
private void createDummyRecord(String url, String code) {
// Add a dummy object for pointer
byte[] dummyFileContentBytes = new String(code).getBytes(StandardCharsets.UTF_8);
InputStream fileInputStream = new ByteArrayInputStream(dummyFileContentBytes);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/html");
metadata.setContentLength(dummyFileContentBytes.length);
PutObjectRequest putObjectRequest = new PutObjectRequest(this.bucket + "-dummy", code
+ Base64.encodeBase64String(url.getBytes()), fileInputStream, metadata);
this.s3Client.putObject(putObjectRequest);
}
示例12: run
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
@Override
public void run() {
ObjectMetadata meta_data = new ObjectMetadata();
if (p_content_type != null)
meta_data.setContentType(p_content_type);
meta_data.setContentLength(p_size);
PutObjectRequest putObjectRequest = new PutObjectRequest(p_bucket_name, p_s3_key, p_file_stream, meta_data);
putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
PutObjectResult res = s3Client.putObject(putObjectRequest);
}
示例13: store
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
public void store() throws IOException {
log.debug("Storing: {}/{}", bucket, key);
ByteArrayOutputStream bufferStream = new ByteArrayOutputStream();
store(bufferStream, null);
byte[] buffer = bufferStream.toByteArray();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(buffer.length);
s3.putObject(bucket, key, new ByteArrayInputStream(buffer), metadata);
}
示例14: initMock
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
@BeforeAll
static void initMock() {
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(100);
when(amazonS3.getObjectMetadata(anyString(), anyString()))
.thenReturn(meta);
}
示例15: putObject
import com.amazonaws.services.s3.model.ObjectMetadata; //導入方法依賴的package包/類
private void putObject() throws IOException {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing regular upload for bucket '{}' key '{}'", bucket,
key);
}
final ObjectMetadata om = createDefaultMetadata();
om.setContentLength(buffer.size());
final PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key,
new ByteArrayInputStream(buffer.toByteArray()), om);
putObjectRequest.setCannedAcl(cannedACL);
putObjectRequest.setGeneralProgressListener(progressListener);
ListenableFuture<PutObjectResult> putObjectResult =
executorService.submit(new Callable<PutObjectResult>() {
@Override
public PutObjectResult call() throws Exception {
return client.putObject(putObjectRequest);
}
});
//wait for completion
try {
putObjectResult.get();
} catch (InterruptedException ie) {
LOG.warn("Interrupted object upload:" + ie, ie);
Thread.currentThread().interrupt();
} catch (ExecutionException ee) {
throw new IOException("Regular upload failed", ee.getCause());
}
}