OBS JAVA SDK Demo:文件语义桶之修改写、追加写
【摘要】 1 背景介绍OBS 文件语义桶支持文件系统语义,提供文件修改写、rename等功能。下面代码展示OBS SDK使用文件语义桶进行修改写、追加写的功能。2 代码展示2.1 文件语义桶之修改写需要先下载OBS最新的JAVA SDK,参考官网链接:https://developer.huaweicloud.com/sdk?OBS,并在自己的项目中导入SDK中的JAR包...
1 背景介绍
OBS 文件语义桶支持文件系统语义,提供文件修改写、rename等功能。下面代码展示OBS SDK使用文件语义桶进行修改写、追加写的功能。
2 代码展示
2.1 文件语义桶之修改写
需要先下载OBS最新的JAVA SDK,参考官网链接:https://developer.huaweicloud.com/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.com/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
第二步:修改写文件obs-testfile,从Position(2)开始,修改内容为Hi,修改后文件obs-testfile,内容为HeHio OBS
3.2 文件语义桶之追加写
第一步:上传文件obs-testfile,内容为Hello OBS
第二步:追加写文件obs-testfile后,文件内容为Hello OBSHi Huawei
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)