Search in sources :

Example 6 with GeneratePresignedUrlRequest

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());
    }
}
Also used : GeneratePresignedUrlRequest(com.aliyun.oss.model.GeneratePresignedUrlRequest) OSSObject(com.aliyun.oss.model.OSSObject) HashMap(java.util.HashMap) IOException(java.io.IOException) Date(java.util.Date) URL(java.net.URL) OSSException(com.aliyun.oss.OSSException) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 7 with GeneratePresignedUrlRequest

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());
    }
}
Also used : GeneratePresignedUrlRequest(com.aliyun.oss.model.GeneratePresignedUrlRequest) OSSObject(com.aliyun.oss.model.OSSObject) Date(java.util.Date) URL(java.net.URL) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONException(org.codehaus.jettison.json.JSONException) Test(org.junit.Test)

Example 8 with GeneratePresignedUrlRequest

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());
    }
}
Also used : HashMap(java.util.HashMap) PutObjectResult(com.aliyun.oss.model.PutObjectResult) OSS(com.aliyun.oss.OSS) Date(java.util.Date) URL(java.net.URL) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) GeneratePresignedUrlRequest(com.aliyun.oss.model.GeneratePresignedUrlRequest) File(java.io.File) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Ignore(org.junit.Ignore)

Aggregations

GeneratePresignedUrlRequest (com.aliyun.oss.model.GeneratePresignedUrlRequest)8 URL (java.net.URL)8 Date (java.util.Date)8 Test (org.junit.Test)6 OSSObject (com.aliyun.oss.model.OSSObject)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 OSSException (com.aliyun.oss.OSSException)3 Ignore (org.junit.Ignore)3 OSS (com.aliyun.oss.OSS)2 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)2 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)2 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)2 PutObjectResult (com.aliyun.oss.model.PutObjectResult)2 InputStream (java.io.InputStream)2 Callback (com.aliyun.oss.model.Callback)1 ResponseHeaderOverrides (com.aliyun.oss.model.ResponseHeaderOverrides)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1