华为云对象存储服务(OBS)

华为云对象存储服务(OBS)

前言

华为云为开发者提供了丰富的 Java SDK,借助这些 SDK 能够方便地与华为云的各类服务进行交互。下面以 华为云对象存储服务(OBS) 的 Java SDK 为例,介绍其使用步骤。

华为云官方 API 文档

更多 API 具体使用方法,请参考华为云官方 API 文档:

https://support.huaweicloud.com/sdk-java-devg-obs/obs_21_0002.html

华为云对象存储 - 文件流形式上传

1、添加依赖

<dependency>
    <groupId>com.obsgroupId>
    <artifactId>obs-java-sdkartifactId>
    <version>3.20.7version>
dependency>

2、配置华为云凭证

你需要准备好华为云的 访问密钥(Access Key)私有密钥(Secret Key),它们可以在华为云控制台的访问密钥管理页面获取。

3、代码示例

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.PutObjectRequest;
import com.obs.services.model.PutObjectResult;

@Service
public class HuaWeiService {

    @Value("${obs.ak:}")
    private String ak;

    @Value("${obs.sk:}")
    private String sk;

    @Value("${obs.endpoint:}")
    private String endPoint;

    @Value("${obs.bucketname:}")
    private String bucketName;

    public void uploadToHuaWei(String detailContent, String fileMode) {

        InputStream inputStream = null;

        // 初始化OBS客户端
        ObsClient obsClient = new ObsClient(ak, sk, endPoint);

        try {
            inputStream = getInputStream(detailContent, fileMode);
            // 对象键(即上传到 OBS 后的文件名)
            String objectKey = "test.txt";

            // 创建上传请求参数
            PutObjectRequest request = new PutObjectRequest();
            request.setBucketName(bucketName);
            request.setObjectKey(objectKey);
            request.setInput(inputStream);

            // 执行上传操作
            PutObjectResult result = obsClient.putObject(request);

        } catch (IOException ioe) {
            // TODO: 异常处理
        } catch (ObsException obsException) {
            // TODO: 异常处理
        } catch (Exception e) {
            // TODO: 异常处理
        } finally {
            // 关闭 文件流
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // TODO: 异常处理
                }
            }

            // 关闭 OBS 客户端
            if (obsClient != null) {
                try {
                    obsClient.close();
                } catch (IOException e) {
                    // TODO: 异常处理
                }
            }
        }

    }

    /**
     * 
     * @Description:根据不同文件类型获取文件流
     * @throws
     */
    private InputStream getInputStream(String detailContent, String fileMode) throws IOException {
        InputStream inputStream = null;
        if ("base64".equals(fileMode)) {
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] fileByte = decoder.decode(detailContent.replaceAll("\r\n", ""));
            inputStream = getInputStream(fileByte);
        } else if ("string".equals(fileMode)) {
            byte[] fileByte = detailContent.getBytes();
            inputStream = getInputStream(fileByte);
        } else if ("url".equals(fileMode)) {
            URL url = new URL(detailContent);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(Integer.valueOf(5000));
            inputStream = conn.getInputStream();
        }
        return inputStream;
    }

    /**
     * 
     * @Description:获取文件流
     * @throws
     */
    private InputStream getInputStream(byte[] fileByte) {
        return new ByteArrayInputStream(fileByte);
    }

}

你可能感兴趣的:(java基础知识,SSM,框架相关,微服务,华为云,python,servlet)