aliyun oss操作汇总

// 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;
    }
}
时间: 2025-01-02 19:20:13

aliyun oss操作汇总的相关文章

aliyun oss js直传且使用服务器端生成签名

采用JS客户端直接签名有一个很严重的安全隐患.就是OSS AccessId/AccessKey暴露在前端页面.可以随意拿到AccessId/AccessKey,这是非常不安全的做法. 这里的签名采用服务端方式生成,前端通过ajax获取. 在阿里云js直传文档范例里,签名生成只有java,php,python,go四种语言的例子,没有c#签名示例.这里用c#实现一个 其他语言的范例地址:https://help.aliyun.com/document_detail/31926.html?spm=5

Aliyun OSS SDK 异步分块上传导致应用异常退出

问题描述: 使用Aliyun OSS SDK的BeginUploadPart/EndUploadPart执行异步分块上传操作,程序出现错误并异常退出! 原因分析: Using .NET Framework 2.0, unhandled exceptions, no matter where they come from, will cause termination of the app. (详见:Exceptions in Managed Threads:https://msdn.micros

SQL字符串操作汇总

--将字符串中从某个字符开始截取一段字符,然后将另外一个字符串插入此处 select stuff('hello,world!',4,4,'****') --返回值hel****orld! --返回从指定位置开始指定长度的字符串 select substring('Hello,World!',2,10) --返回值ello,World --将字符串中某段字符替换为指定的字符串 select replace('hello,world!','ll','aa') --返回值heaao,world! --

提升效率的Linux终端快捷操作汇总

很多普通 Linux 桌面用户都对使用终端感到排斥和恐惧,其实它没大家想的那么复杂,很多常见操作都可以直接在终端中进行,如:安装软件.升级系统等. 无论你是新手还是 Linux 终端使用的老鸟,系统极客在此为大家总结了提升终端命令执行效率的快捷操作汇总,希望能帮助你学习和提升效率. 移动定位光标 在终端中移动光标和定位似乎非常不便,其实不是你想的那样,有很多种方式可以让键盘成为你的好朋友,只是需要掌握正确的方法而已. 定位单词 在长段的命令中,使用 Ctrl + ← 和 Ctrl + → 可快速

JS数组(Array)操作汇总

1.去掉重复的数组元素.2.获取一个数组中的重复项.3.求一个字符串的字节长度,一个英文字符占用一个字节,一个中文字符占用两个字节.4.判断一个字符串中出现次数最多的字符,统计这个次数.5.数组排序. 6.快排. 7.删除/添加数组项. 8.数组随机顺序输出. 9.数组求和.最大值. 10.判断是否为数组. 11.冒泡排序. 1.去掉重复的数组元素. Array.prototype.unique = function() { var ret = []; var o = {}; for(var i

Scala中List、Map、Set各类型操作汇总

1.Scala中List.Map.Set等各类型函数操作汇总 package com.scala.study import scala.collection.immutable.{Queue, TreeMap}import scala.collection.mutable /**  * Created by HP-PC on 2016/5/26.  */ object ScalaCaseDemo {  def main(args: Array[String]): Unit = {    prin

【转】C#路径/文件/目录/I/O常见操作汇总

文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供给你一点有益的思路,如果你有好的建议,恳请能够留言,使这些内容更加完善. 主要内容: 一.路径的相关操作, 如判断路径是否合法,路径类型,路径的特定部分,合并路径,系统文件夹路径等内容: 二.相关通用文件对话框,这些对话框可以帮助我们操作文件系统中的文件和目录: 三.文件.目录.驱动器的操作,如获取

多项式操作汇总

多项式操作汇总 最近学了不少多项式的小 trick,感觉不记一下容易忘.那我就把做法都摆在这里吧,代码还是自己去写了.(鉴于所有操作都是建立在 FFT 之上的,所以会写 FFT 的话所有东西的代码都能写出来了) 多项式乘法 这个就是一切的基础,先把这个代码搞熟后面就都能写啦. 多项式逆元 定义多项式 \(g(x)\) 的逆元 \(g^{-1}(x)\)(以下为了方便简记为 \(f(x)\))是满足如下等式的多项式: \[ \begin{matrix} g(x) \cdot f(x) \equiv

aliyun TableStore相关操作汇总

总结:这个东西本身可能技术还不成熟,使用的人少,有问题很验证解决 遇到的问题:(1)没有一个GUI工具,使用门槛高(2)查询的GetRange不方便,把查询出来的数据使用System.out.println打印出来的是乱码(3)batch insert时报错 2017-05-18 18:14:55.520 ERROR [http-nio-5000-exec-1]com.xiaoyi.app.tool.car.service.BatchService -/862442030078001_23781