java集成华为云obs上传下载实战
说明
最近项目上需要开发一个服务去和华为云OBS集成获取一些业务上的文件,此处记录一下简单的java集成obs的入门,希望对大家快速入门有所帮助:)
实现效果
- 上传对象
- 下载到本地
操作步骤
1.开通obs
上华为云官网,注册账号后购买,支付后进入控制台。
2.获取ak/sk等信息
参考:
https://support.huaweicloud.com/eihealth_faq/eihealth_27_0007.html
3.获取官方java demo
https://github.com/huaweicloud/huaweicloud-sdk-java-obs/blob/master/app/src/test/java/samples_java
我这里拿的是:DownloadSample.java
/**
* This sample demonstrates how to download an object
* from OBS in different ways using the OBS SDK for Java.
*/
public class DownloadSample
{
private static final String endPoint = "https://obs.cn-east-3.myhuaweicloud.com";
private static final String ak = "替换";
private static final String sk = "替换";
private static ObsClient obsClient;
private static String bucketName = "blogstore";
private static String objectKey = "stringdemo";
private static String localFilePath = "d:/temp/" + objectKey;
public static void main(String[] args)
throws IOException
{
ObsConfiguration config = new ObsConfiguration();
config.setSocketTimeout(30000);
config.setConnectionTimeout(10000);
config.setEndPoint(endPoint);
try
{
/*
* Constructs a obs client instance with your account for accessing OBS
*/
obsClient = new ObsClient(ak, sk, config);
/*
* Create bucket
*/
System.out.println("Create a new bucket for demo\n");
//obsClient.createBucket(bucketName);
/*
* Upload an object to your bucket
*/
System.out.println("Uploading a new object to OBS from a file\n");
obsClient.putObject(bucketName, objectKey, createSampleFile());
System.out.println("Downloading an object\n");
/*
* Download the object as an inputstream and display it directly
*/
simpleDownload();
File localFile = new File(localFilePath);
if (!localFile.getParentFile().exists())
{
localFile.getParentFile().mkdirs();
}
System.out.println("Downloading an object to file:" + localFilePath + "\n");
/*
* Download the object to a file
*/
downloadToLocalFile();
System.out.println("Deleting object " + objectKey + "\n");
//obsClient.deleteObject(bucketName, objectKey, null);
}
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
{
/*
* Close obs client
*/
obsClient.close();
}
catch (IOException e)
{
}
}
}
}
private static void downloadToLocalFile()
throws ObsException, IOException
{
ObsObject obsObject = obsClient.getObject(bucketName, objectKey, null);
ReadableByteChannel rchannel = Channels.newChannel(obsObject.getObjectContent());
ByteBuffer buffer = ByteBuffer.allocate(4096);
WritableByteChannel wchannel = Channels.newChannel(new FileOutputStream(new File(localFilePath)));
while (rchannel.read(buffer) != -1)
{
buffer.flip();
wchannel.write(buffer);
buffer.clear();
}
rchannel.close();
wchannel.close();
}
private static void simpleDownload()
throws ObsException, IOException
{
ObsObject obsObject = obsClient.getObject(bucketName, objectKey, null);
displayTextInputStream(obsObject.getObjectContent());
}
private static void displayTextInputStream(InputStream input)
throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
while (true)
{
String line = reader.readLine();
if (line == null)
break;
System.out.println("\t" + line);
}
System.out.println();
reader.close();
}
private static File createSampleFile()
throws IOException
{
File file = File.createTempFile("obs-java-sdk-", ".txt");
file.deleteOnExit();
Writer writer = new OutputStreamWriter(new FileOutputStream(file));
writer.write("abcdefghijklmnopqrstuvwxyz\n");
writer.write("0123456789011234567890\n");
writer.close();
return file;
}
}
4.运行demo
创建工程添加pom依赖:
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>esdk-obs-java-bundle</artifactId>
<version>[3.21.8,)</version>
</dependency>
修改样例代码中的配置项信息,运行。
常用链接
1.获取地区endpoint
https://developer.huaweicloud.com/endpoint
2.spingboot集成obs
https://www.dandelioncloud.cn/article/details/1389618552004751362
常见错误
1.endpoint地区不对
Response Code: 400
Error Message: The location constraint is incompatible for the region specific endpoint this request was sent to.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了