栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java集成华为云obs上传下载实战

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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
        {
            
            obsClient = new ObsClient(ak, sk, config);
            
            
            System.out.println("Create a new bucket for demon");
            //obsClient.createBucket(bucketName);
            
            
            System.out.println("Uploading a new object to OBS from a filen");
            obsClient.putObject(bucketName, objectKey, createSampleFile());
            
            System.out.println("Downloading an objectn");
            
            
            simpleDownload();
            
            File localFile = new File(localFilePath);
            if (!localFile.getParentFile().exists())
            {
                localFile.getParentFile().mkdirs();
            }
            
            System.out.println("Downloading an object to file:" + localFilePath + "n");
            
            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
                {
                    
                    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("abcdefghijklmnopqrstuvwxyzn");
        writer.write("0123456789011234567890n");
        writer.close();
        
        return file;
    }
    
}
4.运行demo

创建工程添加pom依赖:


	    com.huaweicloud
	    esdk-obs-java-bundle
	    [3.21.8,)
	

修改样例代码中的配置项信息,运行。

常用链接 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.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737344.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号