use of com.aliyun.oss.model.GeneratePresignedUrlRequest in project aliyun-oss-java-sdk by aliyun.
the class OSSClientRequestTest method testGeneratePresignedUrlRequest.
@Test
public void testGeneratePresignedUrlRequest() throws Exception {
URI endpoint = new URI("http://localhost/");
String accessId = "test";
String accessKey = "test";
String bucketName = accessId + "bucket";
String objectKey = "object";
String server = endpoint.toString();
OSS ossClient = new OSSClientBuilder().build(server, accessId, accessKey);
URL url = null;
// Simple case
// 10 ειθΏζ
Date expiration = new Date(new Date().getTime() + 1000 * 60 * 10);
try {
url = ossClient.generatePresignedUrl(bucketName, objectKey, expiration);
} catch (ClientException e) {
fail(e.getMessage());
}
String expectedUrlPrefix = endpoint.getScheme() + "://" + endpoint.getHost() + "/" + bucketName + "/" + objectKey + "?Expires=" + Long.toString(expiration.getTime() / 1000) + "&OSSAccessKeyId=" + accessId + "&Signature=";
Assert.assertTrue(url.toString().startsWith(expectedUrlPrefix));
// Override response header
ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();
responseHeaders.setCacheControl("no-cache");
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectKey);
request.setExpiration(expiration);
request.setResponseHeaders(responseHeaders);
expectedUrlPrefix = endpoint.getScheme() + "://" + endpoint.getHost() + "/" + bucketName + "/" + objectKey + "?Expires=" + Long.toString(expiration.getTime() / 1000) + "&OSSAccessKeyId=" + accessId + "&Signature=";
try {
url = ossClient.generatePresignedUrl(request);
Assert.assertTrue(url.toString().startsWith(expectedUrlPrefix));
} catch (ClientException e) {
fail(e.getMessage());
}
}
use of com.aliyun.oss.model.GeneratePresignedUrlRequest in project aliyun-oss-java-sdk by aliyun.
the class OSSClientTest method testGeneratePresignedUrl.
@Test
@Ignore
public /**
* TODO: needs the fix about local time.
*/
void testGeneratePresignedUrl() throws IOException {
OSS client = new OSSClientBuilder().build("oss.aliyuncs.com", "id", "key");
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest("bucket", "key");
Calendar ex = Calendar.getInstance();
ex.set(2015, 1, 1, 0, 0, 0);
Date expiration = ex.getTime();
request.setExpiration(expiration);
request.setContentMD5("md5");
request.setContentType("type");
assertEquals(request.getContentType(), "type");
assertEquals(request.getContentMD5(), "md5");
URL url = client.generatePresignedUrl(request);
assertEquals(url.getPath(), "/key");
assertEquals(url.getAuthority(), "bucket.oss.aliyuncs.com");
assertEquals(url.getHost(), "bucket.oss.aliyuncs.com");
assertEquals(url.getDefaultPort(), 80);
assertEquals(url.getProtocol(), "http");
assertEquals(url.getQuery(), "Expires=1422720000&OSSAccessKeyId=id&Signature=XA8ThdVKdJQ4vlkoggdzCs5s1RY%3D");
assertEquals(url.getFile(), "/key?Expires=1422720000&OSSAccessKeyId=id&Signature=XA8ThdVKdJQ4vlkoggdzCs5s1RY%3D");
request.setContentMD5("md5'");
url = client.generatePresignedUrl(request);
assertTrue(!url.getQuery().equals("Expires=1422720000&OSSAccessKeyId=id&Signature=XA8ThdVKdJQ4vlkoggdzCs5s1RY%3D"));
request.setContentMD5("md5'");
url = client.generatePresignedUrl(request);
assertTrue(!url.getQuery().equals("Expires=1422720000&OSSAccessKeyId=id&Signature=XA8ThdVKdJQ4vlkoggdzCs5s1RY%3D"));
request.setContentType("type'");
request.setContentMD5("md5");
url = client.generatePresignedUrl(request);
assertTrue(!url.getQuery().equals("Expires=1422720000&OSSAccessKeyId=id&Signature=XA8ThdVKdJQ4vlkoggdzCs5s1RY%3D"));
}
use of com.aliyun.oss.model.GeneratePresignedUrlRequest in project aliyun-oss-java-sdk by aliyun.
the class UrlSignatureTest method testGetObject.
@Ignore
public void testGetObject() {
try {
Date expiration = DateUtil.parseRfc822Date("Thu, 19 Mar 2015 18:00:00 GMT");
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
request.setExpiration(expiration);
request.setContentType("application/octet-stream");
URL signedUrl = client.generatePresignedUrl(request);
System.out.println("signed url for getObject: " + signedUrl);
Map<String, String> customHeaders = new HashMap<String, String>();
customHeaders.put("Range", "bytes=100-1000");
customHeaders.put("Content-Type", "application/octet-stream");
OSSObject object = client.getObject(signedUrl, customHeaders);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String expectedETag = null;
try {
int bufSize = 1024 * 4;
byte[] buffer = new byte[bufSize];
int bytesRead;
while ((bytesRead = object.getObjectContent().read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
expectedETag = BinaryUtil.encodeMD5(outputStream.toByteArray());
} catch (IOException e) {
Assert.fail(e.getMessage());
} finally {
IOUtils.safeClose(outputStream);
IOUtils.safeClose(object.getObjectContent());
}
ObjectMetadata metadata = client.getObjectMetadata(bucketName, key);
String actualETag = metadata.getETag();
Assert.assertEquals(expectedETag, actualETag);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.model.GeneratePresignedUrlRequest in project aliyun-oss-java-sdk by aliyun.
the class PutObjectTest method testPutObjectByUrlSignature.
@Test
public void testPutObjectByUrlSignature() throws Exception {
final String key = "put-object-by-urlsignature";
final String metaKey0 = "author";
final String metaValue0 = "aliy";
final String expirationString = "Sun, 12 Apr 2018 12:00:00 GMT";
// 128KB
final long inputStreamLength = 128 * 1024;
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.PUT);
Date expiration = DateUtil.parseRfc822Date(expirationString);
request.setExpiration(expiration);
request.setContentType(DEFAULT_OBJECT_CONTENT_TYPE);
request.addUserMetadata(metaKey0, metaValue0);
URL signedUrl = ossClient.generatePresignedUrl(request);
Map<String, String> requestHeaders = new HashMap<String, String>();
requestHeaders.put(HttpHeaders.CONTENT_TYPE, DEFAULT_OBJECT_CONTENT_TYPE);
requestHeaders.put(OSS_USER_METADATA_PREFIX + metaKey0, metaValue0);
// Override 1
InputStream instream = null;
try {
instream = genFixedLengthInputStream(inputStreamLength);
// Using url signature & chunked encoding to upload specified inputstream.
ossClient.putObject(signedUrl, instream, -1, requestHeaders, true);
OSSObject o = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, o.getKey());
Assert.assertEquals(inputStreamLength, o.getObjectMetadata().getContentLength());
Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
ObjectMetadata metadata = o.getObjectMetadata();
Assert.assertEquals(DEFAULT_OBJECT_CONTENT_TYPE, metadata.getContentType());
Assert.assertTrue(metadata.getUserMetadata().containsKey(metaKey0));
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
// Override 2
try {
instream = genFixedLengthInputStream(inputStreamLength);
// Using url signature encoding to upload specified inputstream.
ossClient.putObject(signedUrl, instream, -1, requestHeaders);
OSSObject o = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, o.getKey());
Assert.assertEquals(inputStreamLength, o.getObjectMetadata().getContentLength());
Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
ObjectMetadata metadata = o.getObjectMetadata();
Assert.assertEquals(DEFAULT_OBJECT_CONTENT_TYPE, metadata.getContentType());
Assert.assertTrue(metadata.getUserMetadata().containsKey(metaKey0));
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
// Override 3
String filePath = genFixedLengthFile(inputStreamLength);
try {
// Using url signature encoding to upload specified inputstream.
ossClient.putObject(signedUrl, filePath, requestHeaders);
OSSObject o = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, o.getKey());
Assert.assertEquals(inputStreamLength, o.getObjectMetadata().getContentLength());
Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
ObjectMetadata metadata = o.getObjectMetadata();
Assert.assertEquals(DEFAULT_OBJECT_CONTENT_TYPE, metadata.getContentType());
Assert.assertTrue(metadata.getUserMetadata().containsKey(metaKey0));
} catch (Exception ex) {
Assert.fail(ex.getMessage());
} finally {
removeFile(filePath);
}
// Override 4
filePath = genFixedLengthFile(inputStreamLength);
try {
// Using url signature encoding to upload specified inputstream.
ossClient.putObject(signedUrl, filePath, requestHeaders, true);
OSSObject o = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, o.getKey());
Assert.assertEquals(inputStreamLength, o.getObjectMetadata().getContentLength());
Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
ObjectMetadata metadata = o.getObjectMetadata();
Assert.assertEquals(DEFAULT_OBJECT_CONTENT_TYPE, metadata.getContentType());
Assert.assertTrue(metadata.getUserMetadata().containsKey(metaKey0));
} catch (Exception ex) {
Assert.fail(ex.getMessage());
} finally {
removeFile(filePath);
}
}
use of com.aliyun.oss.model.GeneratePresignedUrlRequest in project aliyun-oss-java-sdk by aliyun.
the class CallbackTest method testGeneratePresignedUrlWithCallback.
@Test
public void testGeneratePresignedUrlWithCallback() {
String key = "generate-presigned-url-callback";
try {
// callback
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&" + "mimeType=${mimeType}&my_var1=${x:var1}&my_var2=${x:var2}");
callback.addCallbackVar("x:var1", "value1");
callback.addCallbackVar("x:var2", "value2");
callback.setCalbackBodyType(CalbackBodyType.URL);
// generate put url
Map<String, String> cbHeaders = new HashMap<String, String>();
OSSUtils.populateRequestCallback(cbHeaders, callback);
Date expiration = new Date(new Date().getTime() + 3600 * 1000);
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.PUT);
request.setExpiration(expiration);
request.setContentType("text/plain");
request.setHeaders(cbHeaders);
request.addHeader("x-oss-meta-author", "mingdi");
URL signedUrl = ossClient.generatePresignedUrl(request);
System.out.println("SignedUrl:" + signedUrl);
// put with url
Map<String, String> customHeaders = new HashMap<String, String>(cbHeaders);
customHeaders.put("Content-Type", "text/plain");
customHeaders.put("x-oss-meta-author", "mingdi");
InputStream instream = genFixedLengthInputStream(instreamLength);
PutObjectResult putResult = ossClient.putObject(signedUrl, instream, instreamLength, customHeaders);
// check callback body
byte[] buffer = new byte[bufferLength];
int nRead = putResult.getCallbackResponseBody().read(buffer);
putResult.getCallbackResponseBody().close();
Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
// get object and check
OSSObject ossObject = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, ossObject.getKey());
Assert.assertEquals(instreamLength, ossObject.getObjectMetadata().getContentLength());
Assert.assertEquals("mingdi", ossObject.getObjectMetadata().getUserMetadata().get("author"));
ossObject.getObjectContent().close();
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail(ex.getMessage());
}
}
Aggregations