aliyun oss操作汇总
2017-05-24 19:30
134 查看
// endpoint以杭州为例,其它region请按实际情况填写 String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; // accessKey请登录https://ak-console.aliyun.com/#/查看 String accessKeyId = "<yourAccessKeyId>"; String accessKeySecret = "<yourAccessKeySecret>"; String content = "Hello OSS"; // 创建OSSClient实例 OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); AppendObjectRequest appendObjectRequest = new AppendObjectRequest("<yourBucketName>", "<yourKey>", new ByteArrayInputStream(content.getBytes())); // 第一次追加 appendObjectRequest.setPosition(0L); AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest); // 第二次追加 appendObjectRequest.setPosition(appendObjectResult.getNextPosition()); appendObjectResult = ossClient.appendObject(appendObjectRequest); // 第三次追加 appendObjectRequest.setPosition(appendObjectResult.getNextPosition()); appendObjectResult = ossClient.appendObject(appendObjectRequest); // 关闭client ossClient.shutdown();
https://help.aliyun.com/document_detail/32013.html
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.Writer; import com.aliyun.oss.ClientException; import com.aliyun.oss.OSSClient; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.AppendObjectRequest; import com.aliyun.oss.model.AppendObjectResult; import com.aliyun.oss.model.OSSObject; /** * This sample demonstrates how to upload an object by append mode * to Aliyun OSS using the OSS SDK for Java. */ public class AppendObjectSample { private static String endpoint = "*** Provide OSS endpoint ***"; private static String accessKeyId = "*** Provide your AccessKeyId ***"; private static String accessKeySecret = "*** Provide your AccessKeySecret ***"; private static String bucketName = "*** Provide bucket name ***"; private static String key = "*** Provide key ***"; public static void main(String[] args) throws IOException { /* * Constructs a client instance with your account for accessing OSS */ OSSClient client = new OSSClient(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(); } } private static File createTempFile() throws IOException { File file = File.createTempFile("oss-java-sdk-", ".txt"); file.deleteOnExit(); Writer writer = new OutputStreamWriter(new FileOutputStream(file)); writer.write("abcdefghijklmnopqrstuvwxyz\n"); writer.write("0123456789011234567890\n"); writer.close(); return file; } }
相关文章推荐
- Java操作aliyun-OSS对象存储
- aliyun TableStore相关操作汇总
- Eclipse快捷键/快捷操作汇总
- numpy 数组维度操作汇总
- 基于C#生成条形码操作知识汇总附源码下载
- 系统操作指令汇总(运行->输入指令)
- c语言内存操作函数汇总【转】
- 路径,文件,目录,I/O常见操作汇总
- Node.js操作redis示例及常用命令汇总
- 利用itext操作pdf从数据库导出大量数据--功能汇总(一)
- [C#]TreeView操作汇总
- 4用于cifar10的卷积神经网络-4.8/4.9为cifar10卷积网络添加汇总操作上/下
- 转:LinQ操作汇总(From CSharpSamples)
- Java File操作汇总
- sql 获取字符串长度SQL字符串操作汇总
- OSS阿里云对象存储服务,java文件操作
- python 文件查操作汇总
- AIX5.3的磁盘镜像、更换操作汇总
- 数据结构串的操作汇总(未完待续)
- Yii2 使用十六 数据库操作汇总