赞
踩
objectstorage API 官方文档: https://docs.oracle.com/en-us/iaas/api/#/en/objectstorage/20160918/
使用对象存储和存档存储 API 来管理存储桶、对象和相关资源。有关详细信息,请参阅对象存储概述和存档存储概述。
Oracle 云基础设施对象存储服务是一个互联网规模的高性能存储平台,可提供可靠且经济高效的数据持久性。 对象存储服务可以存储无限量的任何内容类型的非结构化数据,包括分析数据和丰富的内容,如图像和视频。
官方解决跨域文档地址: https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/userguide/enabling-cors-examples.html
问题解决及工具类: https://blog.csdn.net/ayunnuo/article/details/127211608
官方文档地址: https://docs.amazonaws.cn/AmazonS3/latest/userguide/upload-objects.html
<!-- 依赖声明 -->
<dependencyManagement>
<dependencies>
<!-- oci -->
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-bom</artifactId>
<version>2.44.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- https://mvnrepository.com/artifact/com.oracle.oci.sdk/oci-java-sdk-objectstorage -->
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-objectstorage</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- hutool工具类 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.3</version>
</dependency>
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* s3基础配置Bean
*
* @author yunnuo <a href="2552846359@qq.com">E-Mail: 2552846359@qq.com</a>
* @since 1.0.0
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "S3基础配置 Bean")
public class S3BaseConfig {
/**
* s3秘密访问密钥
*/
@ApiModelProperty(value = "s3秘密访问密钥")
private String s3SecretAccessKey;
/**
* s3访问密钥id
*/
@ApiModelProperty(value = "s3访问密钥id")
private String s3AccessKeyId;
/**
* s3 bucket
*/
@ApiModelProperty(value = "s3 bucket")
private String s3Bucket;
/**
* 地区
*/
@ApiModelProperty(value = "地区")
private String regions;
/**
* 类型 0=aws环境 1=oracle环境, default= 0
*/
@ApiModelProperty(value = "类型 0=aws环境 1=oracle环境, default=0")
private Integer type = 0;
/**
* aws环境可空, oci环境使用
*/
@ApiModelProperty(value = "namespace")
private String namespace;
@Override
public String toString() {
return JSONUtil.toJsonStr(this);
}
}
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
/**
* s3 配置中心通用配置
*
* @author yunnuo <a href="2552846359@qq.com">Email: 2552846359@qq.com</a>
* @see S3CommonConfig : {@code 参考配置中心key: bt-user.s3.images.config}
* @since 1.0.0
*/
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class S3CommonConfig extends S3BaseConfig {
/**
* 标题
*/
@ApiModelProperty(value = "标题")
private String title;
/**
* 描述
*/
@ApiModelProperty(value = "描述")
private String desc;
/**
* cdn前缀
*/
@ApiModelProperty(value = "cdn前缀")
private String cdnPrefix;
@Override
public String toString() {
return JSONUtil.toJsonStr(this);
}
}
注意: 此工具类生产的预前面上传url 支持跨域操作
import com.kabak.rongsheng.domain.dto.s3.S3BaseConfig;
import com.kabak.rongsheng.domain.dto.s3.S3CommonConfig;
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.objectstorage.ObjectStorageClient;
import com.oracle.bmc.objectstorage.model.CreatePreauthenticatedRequestDetails;
import com.oracle.bmc.objectstorage.model.PreauthenticatedRequest;
import com.oracle.bmc.objectstorage.model.RenameObjectDetails;
import com.oracle.bmc.objectstorage.requests.*;
import com.oracle.bmc.objectstorage.responses.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
/**
* oci Object Storage 版本工具类
*
* @author yunnuo <a href="2552846359@qq.com">Email: 2552846359@qq.com</a>
* @since 1.0.0
*/
public class OciS3Utils {
public static final String PRE_URL = "https://objectstorage.<region_ID>.oraclecloud.com<access-uri><objectName>";
/**
* 复制对象
*
* @param client 客户端
* @param copyObjectRequest 复制对象请求
* @return {@link CopyObjectResponse}
*/
public static CopyObjectResponse copyObject(ObjectStorageClient client, CopyObjectRequest copyObjectRequest) {
return client.copyObject(copyObjectRequest);
}
/**
* 重命名对象
*
* @param client 客户端
* @param namespace 名称空间
* @param bucketName bucket名称
* @param sourceName 原名称
* @param newName 新名字
* @return {@link RenameObjectResponse}
*/
public static RenameObjectResponse renameObject(ObjectStorageClient client, String namespace, String bucketName, String sourceName, String newName) {
RenameObjectDetails renameObjectDetails = RenameObjectDetails.builder()
.sourceName(sourceName)
.newName(newName).build();
RenameObjectRequest renameObjectRequest = RenameObjectRequest.builder()
.namespaceName(namespace)
.bucketName(bucketName)
.renameObjectDetails(renameObjectDetails).build();
return client.renameObject(renameObjectRequest);
}
/**
* 重命名对象
*
* @param client 客户端
* @param renameObjectRequest 重命名对象请求
* @return {@link RenameObjectResponse}
*/
public static RenameObjectResponse renameObject(ObjectStorageClient client, RenameObjectRequest renameObjectRequest) {
return client.renameObject(renameObjectRequest);
}
/**
* 获取对象
*
* @param client 客户端
* @param namespace 名称空间
* @param bucketName bucket名称
* @param key key
* @return {@link GetObjectResponse}
*/
public static GetObjectResponse getObject(ObjectStorageClient client, String namespace, String bucketName, String key) {
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.namespaceName(namespace)
.bucketName(bucketName)
.objectName(key).build();
return client.getObject(getObjectRequest);
}
/**
* 获取对象
*
* @param client 客户端
* @param objectRequest 对象请求
* @return {@link GetObjectResponse}
*/
public static GetObjectResponse getObject(ObjectStorageClient client, GetObjectRequest objectRequest) {
return client.getObject(objectRequest);
}
/**
* 删除对象
*
* @param client 客户端
* @param namespace 名称空间
* @param bucketName bucket名称
* @param key key
* @return {@link DeleteObjectResponse}
*/
public static DeleteObjectResponse deleteObject(ObjectStorageClient client, String namespace, String bucketName, String key) {
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder()
.namespaceName(namespace)
.bucketName(bucketName)
.objectName(key).build();
return client.deleteObject(deleteObjectRequest);
}
/**
* 删除对象
*
* @param client 客户端
* @param deleteObjectRequest 删除对象请求
* @return {@link DeleteObjectResponse}
*/
public static DeleteObjectResponse deleteObject(ObjectStorageClient client, DeleteObjectRequest deleteObjectRequest) {
return client.deleteObject(deleteObjectRequest);
}
/**
* 获取上传单个文件 默认:bundle/date/key 预授权url
* <p color='red'> 注意: 如果需要上传多个文件到同一个目录下, 请使用{@code getPreAuthAnyObjectPathReadWriteURL} </p>
*
* @param client 客户端
* @param config 配置
* @param name 名字(主要是在Console里面起显示作用, 不允许重复)
* @param timeExpires 过期时间
* @param key key
* @return {@link String}
*/
public static String getPreAuthObjDefaultBundleDateURL(ObjectStorageClient client, S3CommonConfig config, String name, Date timeExpires, String... key) {
String filePath = S3CommonUtils.getBundleCurrentDateKey(config.getS3Bucket(), S3CommonUtils.formatFilePath(key));
CreatePreauthenticatedRequestResponse response = getPreAuthAnyObjectReadWriteResponse(client, config.getS3Bucket(), config.getNamespace(), name, filePath, timeExpires);
return getPreAuthUrl(config.getRegions(), response.getPreauthenticatedRequest());
}
/**
* 获取上传单个文件 预授权url
* <p color='red'> 注意: 如果需要上传多个文件到同一个目录下, 请使用{@code getPreAuthAnyObjectPathReadWriteURL} </p>
*
* @param client 客户端
* @param config 配置
* @param name 名字(主要是在Console里面起显示作用, 不允许重复)
* @param timeExpires 过期时间
* @param key key
* @return {@link String}
*/
public static String getPreAuthAnyObjectReadWriteURL(ObjectStorageClient client, S3BaseConfig config, String name, Date timeExpires, String... key) {
String filePath = S3CommonUtils.formatFilePath(key);
CreatePreauthenticatedRequestResponse response = getPreAuthAnyObjectReadWriteResponse(client, config.getS3Bucket(), config.getNamespace(), name, filePath, timeExpires);
return getPreAuthUrl(config.getRegions(), response.getPreauthenticatedRequest());
}
/**
* 获取预身份验证对象路径 [权限: 读写] 默认:bundle/date 预授权url
*
* @param client 客户端
* @param config 配置
* @param name 名字(主要是在Console里面起显示作用, 不允许重复)
* @param timeExpires 过期时间
* @return {@link String}
*/
public static String getPreAuthObjPathDefaultBundleDateURL(ObjectStorageClient client, S3BaseConfig config, String name, String bundle, Date timeExpires) {
String filePath = S3CommonUtils.getBundleCurrentDateKey(bundle, "");
CreatePreauthenticatedRequestResponse response = getPreAuthAnyObjectReadWriteResponse(client, config.getS3Bucket(), config.getNamespace(), name, filePath, timeExpires);
return getPreAuthUrl(config.getRegions(), response.getPreauthenticatedRequest());
}
/**
* 获取预身份验证对象路径[权限: 读写] URL
* <p color='red'>解决性能问题: prefixKey可以传一个前缀路径, 然后通过生成的url地址拼接对象名称进行上传,防止上传多个文件到同一个文件夹下申请多次url</p>
*
* @param client 客户端
* @param config 配置
* @param name 名字(主要是在Console里面起显示作用, 不允许重复)
* @param prefixKey 前缀路径
* @param timeExpires 过期时间
* @return {@link String}
*/
public static String getPreAuthAnyObjectPathReadWriteURL(ObjectStorageClient client, S3BaseConfig config, String name, String prefixKey, Date timeExpires) {
CreatePreauthenticatedRequestResponse response = getPreAuthAnyObjectReadWriteResponse(client, config.getS3Bucket(), config.getNamespace(), name, prefixKey, timeExpires);
return getPreAuthUrl(config.getRegions(), response.getPreauthenticatedRequest());
}
/**
* 获取预身份验证对象路径[权限: 读写] URL
* <p color='red'>解决性能问题: prefixKey可以传一个前缀路径, 然后通过生成的url地址拼接对象名称进行上传,防止上传多个文件到同一个文件夹下申请多次url</p>
*
* @param client 客户端
* @param config 配置
* @param name 名字(主要是在Console里面起显示作用, 不允许重复)
* @param prefixKey 前缀key
* @param timeExpires 过期时间
* @return {@link String}
*/
public static String getPreAuthAnyObjectPathReadWriteURL(ObjectStorageClient client, S3CommonConfig config, String name, String prefixKey, Date timeExpires) {
CreatePreauthenticatedRequestResponse response = getPreAuthAnyObjectReadWriteResponse(client, config.getS3Bucket(), config.getNamespace(), name, prefixKey, timeExpires);
return getPreAuthUrl(config.getRegions(), response.getPreauthenticatedRequest());
}
/**
* 获取预身份验证对象[权限: 读写]响应
*
* @param client 客户端
* @param bucketName bucket名称
* @param namespaceName 名称空间名字
* @param name 名字(主要是在Console里面起显示作用, 不允许重复)
* @param key key
* @param timeExpires 过期时间
* @return {@link CreatePreauthenticatedRequestResponse}
*/
public static CreatePreauthenticatedRequestResponse getPreAuthAnyObjectReadWriteResponse(ObjectStorageClient client, String bucketName, String namespaceName, String name, String key, Date timeExpires) {
CreatePreauthenticatedRequestDetails createPreauthenticatedRequestDetails = CreatePreauthenticatedRequestDetails.builder()
.name(name)
.bucketListingAction(PreauthenticatedRequest.BucketListingAction.ListObjects)
.objectName(key)
.accessType(CreatePreauthenticatedRequestDetails.AccessType.AnyObjectReadWrite)
.timeExpires(timeExpires).build();
CreatePreauthenticatedRequestRequest createPreauthenticatedRequestRequest = CreatePreauthenticatedRequestRequest.builder()
.namespaceName(namespaceName)
.bucketName(bucketName)
.createPreauthenticatedRequestDetails(createPreauthenticatedRequestDetails).build();
return client.createPreauthenticatedRequest(createPreauthenticatedRequestRequest);
}
/**
* 上传对象到默认包/日期/key下
*
* @param config 配置
* @param client 客户端
* @param bundle 包
* @param contentLength 内容长度
* @param contentType 内容类型
* @param key key
* @param objectContent 对象内容
* @return {@link PutObjectResponse}
*/
public static PutObjectResponse putObjectDefaultBundleDate(S3BaseConfig config, ObjectStorageClient client, String bundle, InputStream objectContent, Long contentLength, String contentType, String... key) {
final String bundleCurrentDateKey = S3CommonUtils.getBundleCurrentDateKey(bundle, S3CommonUtils.formatFilePath(key));
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.namespaceName(config.getNamespace())
.bucketName(config.getS3Bucket())
.objectName(bundleCurrentDateKey)
.putObjectBody(objectContent)
.contentLength(contentLength)
.contentType(contentType).build();
return getPutObjectRequest(client, putObjectRequest);
}
/**
* 上传对象到默认包/日期/key下
*
* @param config 配置
* @param client 客户端
* @param bundle 包
* @param contentLength 内容长度
* @param contentType 内容类型
* @param key key
* @param objectContent 对象内容
* @return {@link PutObjectResponse}
*/
public static PutObjectResponse putObjectDefaultBundleDate(S3CommonConfig config, ObjectStorageClient client, String bundle, InputStream objectContent, Long contentLength, String contentType, String... key) {
final String bundleCurrentDateKey = S3CommonUtils.getBundleCurrentDateKey(bundle, S3CommonUtils.formatFilePath(key));
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.namespaceName(config.getNamespace())
.bucketName(config.getS3Bucket())
.objectName(bundleCurrentDateKey)
.putObjectBody(objectContent)
.contentLength(contentLength)
.contentType(contentType).build();
return getPutObjectRequest(client, putObjectRequest);
}
/**
* 上传对象
*
* @param config 配置
* @param client 客户端
* @param contentLength 内容长度
* @param contentType 内容类型
* @param key key
* @param objectContent 对象内容
* @return {@link PutObjectResponse}
*/
public static PutObjectResponse putObject(S3BaseConfig config, ObjectStorageClient client, InputStream objectContent, Long contentLength, String contentType, String... key) {
String filePath = S3CommonUtils.formatFilePath(key);
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.namespaceName(config.getNamespace())
.bucketName(config.getS3Bucket())
.objectName(filePath)
.putObjectBody(objectContent)
.contentLength(contentLength)
.contentType(contentType).build();
return getPutObjectRequest(client, putObjectRequest);
}
/**
* 上传对象
*
* @param config 配置
* @param client 客户端
* @param contentLength 内容长度
* @param contentType 内容类型
* @param key key
* @param objectContent 对象内容
* @return {@link PutObjectResponse}
*/
public static PutObjectResponse putObject(S3CommonConfig config, ObjectStorageClient client, InputStream objectContent, Long contentLength, String contentType, String... key) {
String filePath = S3CommonUtils.formatFilePath(key);
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.namespaceName(config.getNamespace())
.bucketName(config.getS3Bucket())
.objectName(filePath)
.putObjectBody(objectContent)
.contentLength(contentLength)
.contentType(contentType).build();
return getPutObjectRequest(client, putObjectRequest);
}
/**
* 获取 上传对象请求
*
* @param client 客户端
* @param putObjectRequest 把对象请求
* @return {@link PutObjectResponse}
*/
public static PutObjectResponse getPutObjectRequest(ObjectStorageClient client, PutObjectRequest putObjectRequest) {
return client.putObject(putObjectRequest);
}
/**
* 获取默认client
*
* @return {@link ObjectStorageClient}
* @throws IOException ioexception
*/
public static ObjectStorageClient getDefaultClient() throws IOException {
final ConfigFileAuthenticationDetailsProvider provider = getProvider(ConfigFileReader.parseDefault());
return getClient(provider);
}
/**
* 获取client
*
* @param configFilePath 配置文件路径
* @return {@link ObjectStorageClient}
* @throws IOException ioexception
*/
public static ObjectStorageClient getClient(String configFilePath) throws IOException {
final ConfigFileAuthenticationDetailsProvider provider = getProvider(configFilePath);
return getClient(provider);
}
/**
* 获取client
*
* @param configFile 配置文件
* @return {@link ObjectStorageClient}
*/
public static ObjectStorageClient getClient(ConfigFileReader.ConfigFile configFile) {
final ConfigFileAuthenticationDetailsProvider provider = getProvider(configFile);
return getClient(provider);
}
/**
* 获取client
*
* @param provider 提供者
* @return {@link ObjectStorageClient}
*/
public static ObjectStorageClient getClient(ConfigFileAuthenticationDetailsProvider provider) {
return new ObjectStorageClient(provider);
}
/**
* 获取提供者
*
* @param configFilePath 配置文件路径
* @return {@link ConfigFileAuthenticationDetailsProvider}
* @throws IOException ioexception
*/
public static ConfigFileAuthenticationDetailsProvider getProvider(String configFilePath) throws IOException {
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parse(configFilePath);
return getProvider(configFile);
}
/**
* 获取提供者
*
* @param configFile 配置文件
* @return {@link ConfigFileAuthenticationDetailsProvider}
*/
public static ConfigFileAuthenticationDetailsProvider getProvider(ConfigFileReader.ConfigFile configFile) {
return new ConfigFileAuthenticationDetailsProvider(configFile);
}
/**
* 获取预身份验证url
*
* @param region 地区
* @param preauthenticatedRequest preauthenticated请求
* @return {@link String}
*/
public static String getPreAuthUrl(String region, PreauthenticatedRequest preauthenticatedRequest) {
return getPreAuthUrl(region, preauthenticatedRequest.getAccessUri(), preauthenticatedRequest.getObjectName());
}
/**
* 获取预身份验证url
*
* @param accessUri uri访问
* @param objectName 对象名称
* @param region 地区
* @return {@link String}
*/
public static String getPreAuthUrl(String region, String accessUri, String objectName) {
return PRE_URL.replace("<region_ID>", region)
.replace("<access-uri>", accessUri)
.replace("<objectName>", objectName);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。