package com.tzld.piaoquan.tencentad.utils; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.PutObjectRequest; import java.io.File; import java.util.UUID; public class OSSUploader { private static final String ACCESS_KEY_ID = "LTAIP6x1l3DXfSxm"; // 填写您的 Access Key ID private static final String ACCESS_KEY_SECRET = "KbTaM9ars4OX3PMS6Xm7rtxGr1FLon"; // 填写您的 Access Key Secret private static final String ENDPOINT = "oss-cn-hangzhou.aliyuncs.com"; // OSS 访问域名 private static final String BUCKET_NAME = "art-pubbucket"; // 存储空间名称 public static String uploadToOSS(String localVideoPath) { String ossVideoKey = String.format("ad_video_image/%s", UUID.randomUUID()); // 创建 OSSClient 实例 OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET); try { // 创建上传请求 PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, ossVideoKey, new File(localVideoPath)); // 上传文件 ossClient.putObject(putObjectRequest); return "https://rescdn.yishihui.com/" + ossVideoKey; } catch (Exception e) { return null; } finally { // 关闭 OSSClient ossClient.shutdown(); } } public static String uploadToOSS(String target, String localVideoPath) { String ossVideoKey = String.format("%s/%s", target, UUID.randomUUID()); // 创建 OSSClient 实例 OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET); try { // 创建上传请求 PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, ossVideoKey, new File(localVideoPath)); // 上传文件 ossClient.putObject(putObjectRequest); return ossVideoKey; } catch (Exception e) { return null; } finally { // 关闭 OSSClient ossClient.shutdown(); } } public static void main(String[] args) { System.out.println(uploadToOSS("/Users/shimeng/Desktop/1.jpg")); } }