use of com.aliyun.oss.OSSClientBuilder 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.OSSClientBuilder in project aliyun-oss-java-sdk by aliyun.
the class SimpleGetObjectSample method main.
public static void main(String[] args) throws IOException {
/*
* Constructs a client instance with your account for accessing OSS
*/
OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
/**
* Note that there are two ways of uploading an object to your bucket, the one
* by specifying an input stream as content source, the other by specifying a file.
*/
/*
* Upload an object to your bucket from an input stream
*/
System.out.println("Uploading a new object to OSS from an input stream\n");
String content = "Thank you for using Aliyun Object Storage Service";
client.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
/*
* Upload an object to your bucket from a file
*/
System.out.println("Uploading a new object to OSS from a file\n");
client.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
/*
* Download an object from your bucket
*/
System.out.println("Downloading an object");
OSSObject object = client.getObject(new GetObjectRequest(bucketName, key));
System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
displayTextInputStream(object.getObjectContent());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorCode());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
client.shutdown();
}
}
use of com.aliyun.oss.OSSClientBuilder in project aliyun-oss-java-sdk by aliyun.
the class UploadSample method main.
public static void main(String[] args) throws IOException {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
// The local file to upload---it must exist.
uploadFileRequest.setUploadFile(uploadFile);
// Sets the concurrent upload task number to 5.
uploadFileRequest.setTaskNum(5);
// Sets the part size to 1MB.
uploadFileRequest.setPartSize(1024 * 1024 * 1);
// Enables the checkpoint file. By default it's off.
uploadFileRequest.setEnableCheckpoint(true);
UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);
CompleteMultipartUploadResult multipartUploadResult = uploadResult.getMultipartUploadResult();
System.out.println(multipartUploadResult.getETag());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorCode());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} catch (Throwable e) {
e.printStackTrace();
} finally {
ossClient.shutdown();
}
}
use of com.aliyun.oss.OSSClientBuilder in project aliyun-oss-java-sdk by aliyun.
the class AppendObjectSample method main.
public static void main(String[] args) throws IOException {
/*
* Constructs a client instance with your account for accessing OSS
*/
OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
/*
* Append an object from specfied input stream, keep in mind that
* position should be set to zero at first time.
*/
String content = "Thank you for using Aliyun Object Storage Service";
InputStream instream = new ByteArrayInputStream(content.getBytes());
Long firstPosition = 0L;
System.out.println("Begin to append object at position(" + firstPosition + ")");
AppendObjectResult appendObjectResult = client.appendObject(new AppendObjectRequest(bucketName, key, instream).withPosition(0L));
System.out.println("\tNext position=" + appendObjectResult.getNextPosition() + ", CRC64=" + appendObjectResult.getObjectCRC() + "\n");
/*
* Continue to append the object from specfied file descriptor at last position
*/
Long nextPosition = appendObjectResult.getNextPosition();
System.out.println("Continue to append object at last position(" + nextPosition + "):");
appendObjectResult = client.appendObject(new AppendObjectRequest(bucketName, key, createTempFile()).withPosition(nextPosition));
System.out.println("\tNext position=" + appendObjectResult.getNextPosition() + ", CRC64=" + appendObjectResult.getObjectCRC());
/*
* View object type of the appendable object
*/
OSSObject object = client.getObject(bucketName, key);
System.out.println("\tObject type=" + object.getObjectMetadata().getObjectType() + "\n");
// Do not forget to close object input stream if not use it any more
object.getObjectContent().close();
/*
* Delete the appendable object
*/
System.out.println("Deleting an appendable object");
client.deleteObject(bucketName, key);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorCode());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
client.shutdown();
}
}
use of com.aliyun.oss.OSSClientBuilder in project aliyun-oss-java-sdk by aliyun.
the class CRCSample method main.
public static void main(String[] args) throws IOException {
String content = "Hello OSS, Hi OSS, OSS OK.";
// CRC check is enabled by default for upload or download. To turn it off, use the commented code below.
// ClientConfiguration config = new ClientConfiguration();
// config.setCrcCheckEnabled(false);
// OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
// putObject/uploadPart/uploadFile are automatically enabled with CRC.
// However, appendObject needs to call AppendObjectRequest.setInitCRC to enable CRC.
ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
ossClient.deleteObject(bucketName, key);
// First data appending
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes())).withPosition(0L);
// because it's first append, the previous data is empty and thus CRC is 0.
appendObjectRequest.setInitCRC(0L);
AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
// Second data appending
appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes()));
appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
// the initCRC is the first's CRC.
appendObjectRequest.setInitCRC(appendObjectResult.getClientCRC());
appendObjectResult = ossClient.appendObject(appendObjectRequest);
ossClient.deleteObject(bucketName, key);
// Upload with checkpoint, it supports CRC as well.
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
// Sets the upload file with a local file.
uploadFileRequest.setUploadFile(uploadFile);
// Sets the concurrent task number to 5 (default is 1).
uploadFileRequest.setTaskNum(5);
// Sets the part size to 1MB (default is 100K)
uploadFileRequest.setPartSize(1024 * 1024 * 1);
// Enable checkpoint (by default is off for checkpoint enabled upload)
uploadFileRequest.setEnableCheckpoint(true);
ossClient.uploadFile(uploadFileRequest);
// Download with CRC. Note that range download does not support CRC.
OSSObject ossObject = ossClient.getObject(bucketName, key);
Assert.assertNull(ossObject.getClientCRC());
Assert.assertNotNull(ossObject.getServerCRC());
InputStream stream = ossObject.getObjectContent();
while (stream.read() != -1) {
}
stream.close();
// Check if CRC is consistent.
OSSUtils.checkChecksum(IOUtils.getCRCValue(stream), ossObject.getServerCRC(), ossObject.getRequestId());
ossClient.deleteObject(bucketName, key);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorCode());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} catch (InconsistentException ie) {
System.out.println("Caught an OSSException");
System.out.println("Request ID: " + ie.getRequestId());
} catch (Throwable e) {
e.printStackTrace();
} finally {
ossClient.shutdown();
}
}
Aggregations