首先获取 ACCESS_KEYSECRET 与 ACCESS_KEYID
获取 ENDPOINT 与 ALI_DOMAIN 与 BUCKET_NAME(存储空间名称)
代码
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import org.apache.commons.io.FilenameUtils; import org.springframework.web.multipart.MultipartFile; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.UUID; /** * 阿里云视频上传工具 */ public class UploadVideoUtil { //阿里域名=https://+ Bucket 域名 +/ public static final String ALI_DOMAIN = "https://dashanbook.oss-cn-shenzhen.aliyuncs.com/"; //ENDPOINT = https://+ Endpoint(地域节点 外网域名) public static final String ENDPOINT = "https://oss-cn-shenzhen.aliyuncs.com"; public static final String ACCESS_KEYSECRET = "6TPrPrLufY9c5210RqDEk9sMuOLTxH"; // TODO 修改成自己的 public static final String ACCESS_KEYID = "LTAI5tGtUPtiRcmhXjUhEHcU"; // TODO 修改成自己的 //仓库名 public static final String BUCKET_NAME = "dashanbook"; // String object_Name, public static String createOSSClient(MultipartFile multipartFile) { Map<String, Object> map = new HashMap<>(); //生成文件名 String originalFilename = multipartFile.getOriginalFilename(); //原来的图片名 String ext = "." + FilenameUtils.getExtension(originalFilename); String uuid = UUID.randomUUID().toString().replace("-", ""); String fileName = uuid + ext; String endpoint = ENDPOINT; // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。 String accessKeyId = ACCESS_KEYID; String accessKeySecret = ACCESS_KEYSECRET; String bucketName = BUCKET_NAME;//仓库名 // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); String objectName = fileName; try { ossClient.putObject(bucketName, objectName, multipartFile.getInputStream()); } catch (Exception e) { e.printStackTrace(); } finally { Date expiredTime = new Date(System.currentTimeMillis() + 3600L * 1000L); // 关闭OSSClient。 ossClient.shutdown(); // return Result.ok().data(map); return ALI_DOMAIN + objectName; } } }
标签:储存,java,String,OSS,ACCESS,static,import,public From: https://www.cnblogs.com/xbinbin/p/17242955.html