OBS JAVA SDK Demo:文件语义桶之修改写、追加写

举报
lanxinliuli 发表于 2019/07/08 10:13:05 2019/07/08
【摘要】 1 背景介绍OBS 文件语义桶支持文件系统语义,提供文件修改写、rename等功能。下面代码展示OBS SDK使用文件语义桶进行修改写、追加写的功能。2 代码展示2.1 文件语义桶之修改写需要先下载OBS最新的JAVA SDK,参考官网链接:https://developer.huaweicloud.cn/sdk?OBS,并在自己的项目中导入SDK中的JAR包...

1      背景介绍

OBS 文件语义桶支持文件系统语义,提供文件修改写、rename等功能。下面代码展示OBS SDK使用文件语义桶进行修改写、追加写的功能。

2      代码展示

2.1      文件语义桶之修改写

需要先下载OBS最新的JAVA SDK,参考官网链接:https://developer.huaweicloud.cn/sdk?OBS,并在自己的项目中导入SDK中的JAR包。

前提条件:已创建文件语义桶,例如obs-bigdata00-posix

package samples_java;
import java.io.IOException;
import java.io.ByteArrayInputStream;

import com.obs.services.ObsClient;
import com.obs.services.ObsConfiguration;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
import com.obs.services.model.fs.ObsFSFile;
import com.obs.services.model.fs.WriteFileRequest;

public class ObjectTestSample {
   
private static final String endPoint = "XXX";

   
private static final String ak = "XXX";

   
private static final String sk = "XXX";

   
private static ObsClient obsClient;

   
private static String bucketName = "obs-bigdata00-posix";

   
private static String objectKey = "obs-testfile";

   
public static void main(String[] args)
           
throws IOException {
        ObsConfiguration config =
new ObsConfiguration();
        config.setSocketTimeout(
30000);
        config.setConnectionTimeout(
10000);
        config.setEndPoint(
endPoint);
       
try {
            obsClient = new ObsClient(ak, sk, config);

           
String content = "Hello OBS";
           
obsClient.putObject(bucketName,objectKey,new ByteArrayInputStream(content.getBytes("UTF-8")), null);
            ObjectMetadata metadata =
obsClient.getObjectMetadata(bucketName,objectKey);
            WriteFileRequest request =
new WriteFileRequest();
            request.setBucketName(
bucketName);
            request.setObjectKey(
objectKey);
            request.setInput(
new ByteArrayInputStream("Hi".getBytes()));
            request.setPosition(
2);
            ObsFSFile result =
obsClient.writeFile(request);

       
} catch (ObsException e) {
            System.
out.println("Response Code: " + e.getResponseCode());
            System.
out.println("Error Message: " + e.getErrorMessage());
            System.
out.println("Error Code:       " + e.getErrorCode());
            System.
out.println("Request ID:      " + e.getErrorRequestId());
            System.
out.println("Host ID:           " + e.getErrorHostId());
        }
finally {
           
if (obsClient != null) {
               
try {

                   
obsClient.close();
                }
catch (IOException e) {
                    System.
out.println(e);
                }
            }
        }
    }
}


2.2      文件语义桶之追加写

需要先下载OBS最新的JAVA SDK,参考官网链接:https://developer.huaweicloud.cn/sdk?OBS,并在自己的项目中导入SDK中的JAR包。

前提条件:已创建文件语义桶,例如obs-bigdata00-posix

package samples_java;
import java.io.IOException;
import java.io.ByteArrayInputStream;

import com.obs.services.ObsClient;
import com.obs.services.ObsConfiguration;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
import com.obs.services.model.fs.ObsFSFile;
import com.obs.services.model.fs.WriteFileRequest;

public class ObjectTestSample {
   
private static final String endPoint = "XXX";

   
private static final String ak = "XXX";

   
private static final String sk = "XXX";

   
private static ObsClient obsClient;

   
private static String bucketName = "obs-bigdata00-posix";

   
private static String objectKey = "obs-testfile";

   
public static void main(String[] args)
           
throws IOException {
        ObsConfiguration config =
new ObsConfiguration();
        config.setSocketTimeout(
30000);
        config.setConnectionTimeout(
10000);
        config.setEndPoint(
endPoint);
       
try {
           
obsClient = new ObsClient(ak, sk, config);

           
String content = "Hello OBS";
           
obsClient.putObject(bucketName,objectKey,new ByteArrayInputStream(content.getBytes("UTF-8")), null);
            ObjectMetadata metadata =
obsClient.getObjectMetadata(bucketName,objectKey);
            WriteFileRequest request =
new WriteFileRequest();
            request.setBucketName(
bucketName);
            request.setObjectKey(
objectKey);
            request.setInput(
new ByteArrayInputStream("Hi Huawei".getBytes()));
            request.setPosition(metadata.getContentLength());
            ObsFSFile result =
obsClient.appendFile(request);

       
} catch (ObsException e) {
            System.
out.println("Response Code: " + e.getResponseCode());
            System.
out.println("Error Message: " + e.getErrorMessage());
            System.
out.println("Error Code:       " + e.getErrorCode());
            System.
out.println("Request ID:      " + e.getErrorRequestId());
            System.
out.println("Host ID:           " + e.getErrorHostId());
        }
finally {
           
if (obsClient != null) {
               
try {
                   
obsClient.close();
                }
catch (IOException e) {
                    System.
out.println(e);
                }
            }
        }
    }
}


3      效果展示

3.1      文件语义桶之修改写

第一步:上传文件obs-testfile,内容为Hello OBS

image.png

第二步:修改写文件obs-testfile,从Position(2)开始,修改内容为Hi,修改后文件obs-testfile,内容为HeHio OBS

image.png

3.2      文件语义桶之追加写

第一步:上传文件obs-testfile,内容为Hello OBS

image.png

第二步:追加写文件obs-testfile后,文件内容为Hello OBSHi Huawei

image.png

 


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。