0
点赞
收藏
分享

微信扫一扫

知识图谱6:neo4j查询语句

蓝莲听雨 2024-11-18 阅读 6
java

七牛云对象存储操作(QiniuUtil)

  1. 配置:使用 com.qiniu.storage.Configuration 类来配置上传设置,如指定区域(Region)和分片上传版本。
  2. 上传管理器:通过 UploadManager 类来处理文件上传。
  3. 认证:使用 Auth.create() 方法创建认证对象,然后生成上传凭证(upToken)。
  4. 上传:使用 uploadManager.put() 方法上传文件,可以上传字节数组、文件路径或输入流。
  5. 错误处理:捕获 QiniuException 来处理上传过程中可能出现的错误。

阿里云对象存储操作(AliOssUtil)

  1. 配置:使用 OSSClientBuilder 来构建 OSS 客户端实例,需要提供 endpoint、accessKeyId、accessKeySecret。
  2. 上传:使用 ossClient.putObject() 方法上传文件,可以直接上传字节数组或输入流。
  3. 错误处理:捕获 OSSException 和 ClientException 来处理上传过程中可能出现的错误。
  4. 资源管理:在 finally 块中关闭 OSS 客户端实例。

主要区别

  • 客户端构建:七牛云使用 UploadManager 和 Configuration,而阿里云使用 OSSClientBuilder
  • 认证方式:七牛云需要显式生成上传凭证(upToken),而阿里云的认证信息直接在 OSSClientBuilder 中提供。
  • 错误处理:七牛云捕获 QiniuException,阿里云捕获 OSSException 和 ClientException
  • 资源管理:阿里云在 finally 块中显式关闭 OSS 客户端,而七牛云的 UploadManager 通常不需要显式关闭。

注意事项

  • 七牛云的上传凭证(upToken)是临时的,适用于短期的上传操作。
  • 阿里云的 OSS 客户端在不需要时应该关闭,以释放资源。

七牛云对象存储操作 (QiniuUtil)

 
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
@Autowired
private QiniuUtil qiniuOssUtil;
//返回的data是必须的,所以指定泛型为String
//Spring mvc 自动将返回值封装为json 参数名保持一致
@PostMapping("upload")
@ApiOperation("文件上传")
public Result<String> upload(MultipartFile file) throws IOException {
log.info("文件上传:{}",file);
//获取原始文件名
String originalFilename = file.getOriginalFilename();
//获取文件后缀
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
//构建新文件名称
String objectName = UUID.randomUUID().toString() + suffix;
//文件的请求路径 网址
String filePath = qiniuOssUtil.uploadByBytes(file.getBytes() ,objectName);
log.info("文件上传完成,文件访问的url: {}", filePath);
return Result.success(filePath);
}
}
 
package com.sky.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.sky.properties.QiniuOssProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Component
public class QiniuUtil {
@Autowired
private QiniuOssProperties qiniuOssProperties;

public String uploadByBytes(byte[] bytes, String objectName){
//构造一个带指定 Region 对象的配置类
com.qiniu.storage.Configuration cfg = new com.qiniu.storage.Configuration(Region.region2());
cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
//...其他参数参考类注释

UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKeyId = qiniuOssProperties.getAccessKeyId();
String accessKeySecretKey = qiniuOssProperties.getAccessKeySecret();
String bucketName = qiniuOssProperties.getBucketName();
String endpoint = qiniuOssProperties.getEndpoint();
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = objectName;

// byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
Auth auth = Auth.create(accessKeyId, accessKeySecretKey);
String upToken = auth.uploadToken(bucketName);

try {
Response response = uploadManager.put(bytes, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
// 构建文件访问路径
String url = endpoint + "/" + putRet.key;
return url; // 返回文件访问路径
} catch (QiniuException ex) {
ex.printStackTrace();
if (ex.response != null) {
System.err.println(ex.response);

try {
String body = ex.response.toString();
System.err.println(body);
} catch (Exception ignored) {
}
}
return null;
}

}
}

注意 

七牛云 页面无法显示已上传的文件是因为bucket要设置http://      且https://  也是不行的

阿里云对象存储操作 (AliOssUtil)

package com.sky.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayInputStream;

@Data
@AllArgsConstructor
@Slf4j
public class AliOssUtil {

private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;

/**
* 文件上传
*
* @param bytes
* @param objectName
* @return
*/

public String upload(byte[] bytes, String objectName) {

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

try {
// 创建PutObject请求。
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
} 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();
}
}

//文件访问路径规则 https://BucketName.Endpoint/ObjectName
StringBuilder stringBuilder = new StringBuilder("https://");
stringBuilder
.append(bucketName)
.append(".")
.append(endpoint)
.append("/")
.append(objectName);

log.info("文件上传到:{}", stringBuilder.toString());

return stringBuilder.toString();
}
}
举报

相关推荐

0 条评论