use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project extension-s3 by lucee.
the class S3 method write.
public void write(String bucketName, String objectName, Resource res, Object acl, String region) throws IOException {
if (res instanceof File) {
write(bucketName, objectName, (File) res, acl, region);
return;
}
String ct = CFMLEngineFactory.getInstance().getResourceUtil().getMimeType(res, null);
try {
bucketName = improveBucketName(bucketName);
objectName = improveObjectName(objectName, false);
flushExists(bucketName, objectName);
// send request to S3 to create folder
AmazonS3Client client = getAmazonS3(bucketName, region);
if (res.length() > maxSize) {
File tmp = File.createTempFile("writeResource-", ".txt");
try {
Util.copy(res.getInputStream(), new FileOutputStream(tmp), true, true);
write(bucketName, objectName, tmp, acl, region);
return;
} finally {
tmp.delete();
}
} else {
InputStream is = null;
ObjectMetadata md = new ObjectMetadata();
md.setLastModified(new Date());
md.setContentLength(res.length());
try {
PutObjectRequest por = new PutObjectRequest(bucketName, objectName, is = res.getInputStream(), md);
if (acl != null)
setACL(por, acl);
client.putObject(por);
flushExists(bucketName, objectName);
} catch (AmazonServiceException ase) {
if (ase.getErrorCode().equals("EntityTooLarge")) {
S3Exception s3e = toS3Exception(ase);
if (s3e.getProposedSize() != 0 && s3e.getProposedSize() < maxSize) {
maxSize = s3e.getProposedSize();
write(bucketName, objectName, res, acl, region);
return;
}
throw s3e;
}
if (ase.getErrorCode().equals("NoSuchBucket")) {
createDirectory(bucketName, acl, region);
write(bucketName, objectName, res, acl, region);
return;
} else
throw toS3Exception(ase);
} finally {
Util.closeEL(is);
client.release();
}
}
} catch (AmazonServiceException se) {
throw toS3Exception(se);
}
}
use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project extension-s3 by lucee.
the class S3 method getMetaDataStruct.
public Struct getMetaDataStruct(String bucketName, String objectName) throws S3Exception {
Struct sct = CFMLEngineFactory.getInstance().getCreationUtil().createStruct();
bucketName = improveBucketName(bucketName);
objectName = improveObjectName(objectName);
sct.setEL("bucketName", bucketName);
AmazonS3Client client = getAmazonS3(bucketName, null);
try {
if (Util.isEmpty(objectName)) {
// TODO
}
if (objectName != null) {
sct.setEL("objectName", objectName);
S3Object o = client.getObject(bucketName, objectName);
ObjectMetadata md = o.getObjectMetadata();
Map<String, Object> rmd = md.getRawMetadata();
Iterator<Entry<String, Object>> it = rmd.entrySet().iterator();
Entry<String, Object> e;
while (it.hasNext()) {
e = it.next();
sct.setEL(e.getKey().replace('-', '_'), e.getValue());
}
sct.setEL("lastModified", md.getLastModified());
}
} finally {
client.release();
}
/*
* if (owner != null) { sct.put("owner", owner.getDisplayName()); sct.put("owner_id",
* owner.getId()); }
*/
return sct;
}
use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project aws-athena-query-federation by awslabs.
the class S3BlockSpiller method write.
/**
* Writes (aka spills) a Block.
*/
protected SpillLocation write(Block block) {
try {
S3SpillLocation spillLocation = makeSpillLocation();
EncryptionKey encryptionKey = spillConfig.getEncryptionKey();
logger.info("write: Started encrypting block for write to {}", spillLocation);
byte[] bytes = blockCrypto.encrypt(encryptionKey, block);
totalBytesSpilled.addAndGet(bytes.length);
logger.info("write: Started spilling block of size {} bytes", bytes.length);
amazonS3.putObject(spillLocation.getBucket(), spillLocation.getKey(), new ByteArrayInputStream(bytes), new ObjectMetadata());
logger.info("write: Completed spilling block of size {} bytes", bytes.length);
return spillLocation;
} catch (RuntimeException ex) {
asyncException.compareAndSet(null, ex);
logger.warn("write: Encountered error while writing block.", ex);
throw ex;
}
}
use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project Project-Decode by cheery72.
the class UserServiceImpl method AwsFile.
private String AwsFile(MultipartFile file) {
String fileName = createFileName(file.getOriginalFilename());
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(file.getSize());
objectMetadata.setContentType(file.getContentType());
try (InputStream inputStream = file.getInputStream()) {
amazonS3.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata).withCannedAcl(CannedAccessControlList.PublicRead));
} catch (IOException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "파일 업로드에 실패했습니다.");
}
return String.format(url + "/%s", fileName);
}
use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project aws-lambda-developer-guide by awsdocs.
the class Handler method handleRequest.
@Override
public String handleRequest(S3Event s3event, Context context) {
try {
logger.info("EVENT: " + gson.toJson(s3event));
S3EventNotificationRecord record = s3event.getRecords().get(0);
String srcBucket = record.getS3().getBucket().getName();
// Object key may have spaces or unicode non-ASCII characters.
String srcKey = record.getS3().getObject().getUrlDecodedKey();
String dstBucket = srcBucket;
String dstKey = "resized-" + srcKey;
// Infer the image type.
Matcher matcher = Pattern.compile(".*\\.([^\\.]*)").matcher(srcKey);
if (!matcher.matches()) {
logger.info("Unable to infer image type for key " + srcKey);
return "";
}
String imageType = matcher.group(1);
if (!(JPG_TYPE.equals(imageType)) && !(PNG_TYPE.equals(imageType))) {
logger.info("Skipping non-image " + srcKey);
return "";
}
// Download the image from S3 into a stream
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
S3Object s3Object = s3Client.getObject(new GetObjectRequest(srcBucket, srcKey));
InputStream objectData = s3Object.getObjectContent();
// Read the source image
BufferedImage srcImage = ImageIO.read(objectData);
int srcHeight = srcImage.getHeight();
int srcWidth = srcImage.getWidth();
// Infer the scaling factor to avoid stretching the image
// unnaturally
float scalingFactor = Math.min(MAX_WIDTH / srcWidth, MAX_HEIGHT / srcHeight);
int width = (int) (scalingFactor * srcWidth);
int height = (int) (scalingFactor * srcHeight);
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
// Fill with white before applying semi-transparent (alpha) images
g.setPaint(Color.white);
g.fillRect(0, 0, width, height);
// Simple bilinear resize
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImage, 0, 0, width, height, null);
g.dispose();
// Re-encode image to target format
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(resizedImage, imageType, os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
// Set Content-Length and Content-Type
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(os.size());
if (JPG_TYPE.equals(imageType)) {
meta.setContentType(JPG_MIME);
}
if (PNG_TYPE.equals(imageType)) {
meta.setContentType(PNG_MIME);
}
// Uploading to S3 destination bucket
logger.info("Writing to: " + dstBucket + "/" + dstKey);
try {
s3Client.putObject(dstBucket, dstKey, is, meta);
} catch (AmazonServiceException e) {
logger.error(e.getErrorMessage());
System.exit(1);
}
logger.info("Successfully resized " + srcBucket + "/" + srcKey + " and uploaded to " + dstBucket + "/" + dstKey);
return "Ok";
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations