2024 桐庐半程马拉松
00:00:00
时间
0.00
距离(公里)
--:--
配速
--
步频
--
心率 (bpm)
--
配速
步频
|
share-image
ESC

使用网易云 Java sdk 上传文件到某个目录

创建一个 maven 工程在 pom.xml 中加入


<dependency>
<groupId>com.netease.cloud</groupId>
<artifactId>nos-sdk-java-publiccloud</artifactId>
<version>0.0.2</version>
</dependency>



然后建一个 class,代码如下


package test.test;
import java.io.File;
import com.netease.cloud.ClientConfiguration;
import com.netease.cloud.Protocol;
import com.netease.cloud.auth.BasicCredentials;
import com.netease.cloud.auth.Credentials;
import com.netease.cloud.services.nos.NosClient;
import com.netease.cloud.services.nos.transfer.TransferManager;



public class NeteaseNosPutFile {

public static void main(String[] args) {
String accessKey = ""; //AK
String secretKey = ""; //SK
Credentials credentials = new BasicCredentials(accessKey, secretKey);
ClientConfiguration conf = new ClientConfiguration();
// 设置 NosClient 使用的最大连接数
conf.setMaxConnections(200);
// 设置 socket 超时时间
conf.setSocketTimeout(10000);
// 设置失败请求重试次数
conf.setMaxErrorRetry(2);
// 如果要用 https 协议,请加上下面语句
conf.setProtocol(Protocol.HTTPS);

NosClient nosClient = new NosClient(credentials,conf);
nosClient.setEndpoint("nos-eastchina1.126.net");
TransferManager transferManager = new TransferManager(nosClient);
//要上传文件的路径
String filePath = "kafka.pptx";
try {
nosClient.putObject("netease01","a/b/c/d/e/fkafka.pptx", new File(filePath));
}catch (Exception e){
System.out.println(e.getMessage());
}

}

}


主要是这段,加目录的时候在 object 前面加上对应的目录,注意不是绝对路径

nosClient.putObject("netease01","a/b/c/d/e/fkafka.pptx", new File(filePath));
文章作者:阿文
文章链接: https://www.awen.me/post/37867.html
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿文的博客
本文于 2018-03-29 发布,已超过半年(2863天),请注意甄别内容是否已过期。