首页 > 其他分享 >OSS存储文件

OSS存储文件

时间:2024-06-04 20:01:32浏览次数:23  
标签:文件 存储 OSS aliyun println com oss out

阿里云OSS官方文档:

快速使用OSS SDK_对象存储(OSS)-阿里云帮助中心 (aliyun.com)

在Maven工程中使用OSS Java SDK,只需在pom.xml中加入相应依赖即可。在<dependencies>中加入如下内容:

优先使用最新版本的依赖项,此处以3.15.1版本为例。

<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.15.1</version>
</dependency>

 

如果使用的是Java 9及以上的版本,则需要添加JAXB相关依赖。添加JAXB相关依赖示例代码如下:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>
<!-- no more than 2.3.3-->
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.3</version>
</dependency>

上传文件

以下代码用于通过流式上传的方式将文件上传到OSS。

代码运行成功后,exampledir目录下将上传exampleobject.txt文件。

 1 import com.aliyun.oss.ClientException;
 2 import com.aliyun.oss.OSS;
 3 import com.aliyun.oss.OSSClientBuilder;
 4 import com.aliyun.oss.OSSException;
 5 import com.aliyun.oss.common.auth.CredentialsProviderFactory;
 6 import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
 7 import java.io.ByteArrayInputStream;
 8 
 9 public class Demo {
10 
11     public static void main(String[] args) throws Exception {
12         // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
13         String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
14         // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
15         EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
16         // 填写Bucket名称,例如examplebucket。
17         String bucketName = "examplebucket";
18         // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
19         String objectName = "exampledir/exampleobject.txt";
20 
21         // 创建OSSClient实例。
22         OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
23 
24         try {
25             String content = "Hello OSS";
26             ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
27         } catch (OSSException oe) {
28             System.out.println("Caught an OSSException, which means your request made it to OSS, "
29                     + "but was rejected with an error response for some reason.");
30             System.out.println("Error Message:" + oe.getErrorMessage());
31             System.out.println("Error Code:" + oe.getErrorCode());
32             System.out.println("Request ID:" + oe.getRequestId());
33             System.out.println("Host ID:" + oe.getHostId());
34         } catch (ClientException ce) {
35             System.out.println("Caught an ClientException, which means the client encountered "
36                     + "a serious internal problem while trying to communicate with OSS, "
37                     + "such as not being able to access the network.");
38             System.out.println("Error Message:" + ce.getMessage());
39         } finally {
40             if (ossClient != null) {
41                 ossClient.shutdown();
42             }
43         }
44     }
45 }

标签:文件,存储,OSS,aliyun,println,com,oss,out
From: https://www.cnblogs.com/linzepro/p/18231612

相关文章

  • Git 保留空文件夹结构
    假设有如下helloworld项目结构:helloworld|--.git|--.gitignore|--Builds|--WebGL|--iOS|--Android现在有个需求,在上传到github仓库时,只想保留WebGL、iOS、Android文件夹的结构,不想要里面的内容,可以按以下步骤实现分别在WebGL、iOS、Android文件......
  • MySQL的数据在磁盘上如何存储?
    存储引擎百度百科是这样定义存储引擎的:MySQL中的数据用各种不同的技术存储在文件(或者内存)中,这些不同的技术以及配套的相关功能在MySQL中被称作存储引擎。简单来说就是不同的存储引擎,我们的数据存储的格式也会不一样。就好比图片有不同的格式,比如:.jpg,.png,.gif等等……......
  • Java读写xml文件
    前言使用dom4j库java读xml文件示例代码:SAXReadersaxReader=newSAXReader();Documentdoc=null;try{ FileInputStreamfin=newFileInputStream("D://doc_f/student.xml"); document=saxReader.read(fin); Elementroot=document.getRootElement();//获取......
  • 33、nginx-CA和https(http+ssl(安全套接字))私有CA--网站加密(443 ssl https)-加密密钥key
    算法:将原有的值通过一些算法来进行加密对称算法(加密和解密的过程用的钥匙是相同的):AES\DES\3DES非对称算法(加密和解密的过程中使用的钥匙是不一样的):DH\RSAhash算法:MD5=========================================SSL安全套接字===================================......
  • 【转】[VS] 把.nuget文件夹从C盘移到其它盘
    转自:https://blog.csdn.net/Henry_Wu001/article/details/1358256991、用命令查看缓存文件夹的具体路径:dotnetnugetlocalsall--list会出现大概下面的结果: 其中“global-packages:”开头的这行就是这个缓存的位置。上面的图片中,我的是E盘,是因为我已经移动过了。你看到的......
  • 数据泄露怎么防?企业文件加密来帮忙
    在数字化时代,数据泄露事件频发,给企业带来了前所未有的安全挑战。企业的核心数据、商业机密、客户信息等一旦泄露,不仅会导致经济损失,还会损害企业的声誉和客户信任。因此,如何有效防止数据泄露,成为了企业管理的重要课题。一、数据泄露的风险数据泄露可能通过多种途径发生,包括黑客......
  • c#中String和StringBuilder两个对象用户写入文件:导出操作
    在C#中,有两个主要用于字符串操作的类:String和StringBuilder。String:String类表示一个不可变的字符串。这意味着一旦创建了一个字符串对象,它的内容就不能被修改。每次对字符串进行操作(比如连接、替换、删除等),都会创建一个新的字符串对象。这可能会导致性能开销,尤其是在需要......
  • 学习笔记482—手把手教你如何用mac访问win10共享文件
    这个方法巨简单,只需要两台电脑都用同一个网络,我的两台电脑都是连接wifi使用的,跟着图文一步步来操作哦操作步骤:......
  • 【ubuntu使用记录】—— 如何使用vim编译器如何写入保存文件和linux常用的操作指令记
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言vim编译器的使用1.写入的操作2.保存的操作常用的系统操作指令1.查看系统配置2.查看库的版本3.基本操作指令4.解压和压缩相关指令(1)安装rar和unrar工具(2)使用前言在Ubuntu中,使用......
  • 将某xlsx文件转为UTF-8编码格式
    问题:        在使用jupyter对数据进行处理时,导入的文件可能不是UTF-8编码格式,这时就要将文件转为UTF-8。处理:    (1)将文件使用excel打开。    (2)点击左上角文件    (3)选择其他格式,将文件类型选为csvUTF-8格式。    (4)再次将文......