Java阿里云OSS上传文件

第一步:打开阿里云网站

打开网站:阿里云OSS:对象存储 OSS_云存储服务_企业数据管理_存储-阿里云 (aliyun.com)

第二步:创建一个Bucket容器

第三步:这里可以查看自己的AccessKey ID和AccessKeySecret:

记住在创建的时候记得保存AccessKeySecret,现在已经不被允许再次查看

进入之后点击创建:

第四步:在pom.xml文件中引入阿里云依赖

<!--阿里云oss1-->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.15.1</version>
        </dependency>

第五步:创建存储空间。

打开SDK下载,再点开SDK示例

点开:

文档中心打开

找到Java快速入门:

就可以找到以下代码用于创建存储空间。直接拿这个用也是可以的
package com.example;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;

import java.io.FileInputStream;
import java.io.InputStream;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Endpoint以华北2(北京)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        // 阿里云账号
        String accessKeyId = "LTAI5tCvdmnowKuE7AxwQDnV";
        String accessKeySecret = "lbIroYZDWWdb39KcnUwTYKaRh7Iirf";
        // 填写Bucket名称,例如web-tlias-file-start。
        String bucketName = "web-tlias-file-start";
        // 填写Object完整路径,完整路径中不能包含Bucket名称,例如web-tlias-file-start/web-tlias-file-start.txt。
        String objectName = "1.jpg";
        //制定本地路径
        String filePath = "C:\\staticImage\\2.jpg";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            InputStream inputStream = new FileInputStream(filePath);
            // 创建PutObjectRequest对象。
            ossClient.putObject(bucketName, objectName, inputStream);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}   
  • 17
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值