use of com.aliyun.oss.model.GeneratePresignedUrlRequest in project aliyun-oss-java-sdk by aliyun.
the class GetObjectTest method testGetObjectByUrlsignature.
@Test
public void testGetObjectByUrlsignature() {
final String key = "put-object-by-urlsignature";
final String expirationString = "Sun, 12 Apr 2018 12:00:00 GMT";
// 128KB
final long inputStreamLength = 128 * 1024;
final long firstByte = inputStreamLength / 2;
final long lastByte = inputStreamLength - 1;
try {
ossClient.putObject(bucketName, key, genFixedLengthInputStream(inputStreamLength), null);
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key);
Date expiration = DateUtil.parseRfc822Date(expirationString);
request.setExpiration(expiration);
request.setContentType(DEFAULT_OBJECT_CONTENT_TYPE);
URL signedUrl = ossClient.generatePresignedUrl(request);
Map<String, String> requestHeaders = new HashMap<String, String>();
requestHeaders.put(HttpHeaders.RANGE, String.format("bytes=%d-%d", firstByte, lastByte));
requestHeaders.put(HttpHeaders.CONTENT_TYPE, DEFAULT_OBJECT_CONTENT_TYPE);
OSSObject o = ossClient.getObject(signedUrl, requestHeaders);
try {
int bytesRead = -1;
int totalBytes = 0;
byte[] buffer = new byte[4096];
while ((bytesRead = o.getObjectContent().read(buffer)) != -1) {
totalBytes += bytesRead;
}
Assert.assertEquals((lastByte - firstByte + 1), totalBytes);
} catch (IOException e) {
Assert.fail(e.getMessage());
} finally {
IOUtils.safeClose(o.getObjectContent());
}
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.model.GeneratePresignedUrlRequest in project aliyun-oss-java-sdk by aliyun.
the class ImageProcessTest method testGeneratePresignedUrlWithProcess.
@Test
public void testGeneratePresignedUrlWithProcess() {
// 缩放
String style = "image/resize,m_fixed,w_100,h_100";
try {
Date expiration = DateUtil.parseRfc822Date("Wed, 21 Dec 2022 14:20:00 GMT");
GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, originalImage, HttpMethod.GET);
req.setExpiration(expiration);
req.setProcess(style);
URL signedUrl = ossClient.generatePresignedUrl(req);
System.out.println(signedUrl);
OSSObject ossObject = ossClient.getObject(signedUrl, null);
ossClient.putObject(bucketName, newImage, ossObject.getObjectContent());
ImageInfo imageInfo = getImageInfo(bucketName, newImage);
Assert.assertEquals(imageInfo.getHeight(), 100);
Assert.assertEquals(imageInfo.getWidth(), 100);
Assert.assertEquals(imageInfo.getFormat(), "jpg");
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.model.GeneratePresignedUrlRequest in project aliyun-oss-java-sdk by aliyun.
the class UrlSignatureTest method testPutObject.
@Ignore
public void testPutObject() {
OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey);
try {
Date expiration = DateUtil.parseRfc822Date("Thu, 19 Mar 2015 18:00:00 GMT");
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.PUT);
request.setExpiration(expiration);
request.setContentType("application/octet-stream");
request.addUserMetadata("author", "aliy");
URL signedUrl = client.generatePresignedUrl(request);
System.out.println("signed url for putObject: " + signedUrl);
File f = new File(filePath);
FileInputStream fin = new FileInputStream(f);
Map<String, String> customHeaders = new HashMap<String, String>();
customHeaders.put("Content-Type", "application/octet-stream");
customHeaders.put("x-oss-meta-author", "aliy");
PutObjectResult result = client.putObject(signedUrl, fin, f.length(), customHeaders);
fin = new FileInputStream(f);
byte[] binaryData = IOUtils.readStreamAsByteArray(fin);
String expectedETag = BinaryUtil.encodeMD5(binaryData);
String actualETag = result.getETag();
Assert.assertEquals(expectedETag, actualETag);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
Aggregations