阿里云oss上传

public class OssManage { public static String ACCESS_KEY_ID = null; public static String ACCESS_KEY_SECRET = null; public static String ENDPOINT = null; public static String BUCKETNAME = null; public static String BUCKETNAME_VIDEO = null; public static String OUT_OF_DATE = null; private OSSClient client = null; private ObjectMetadata meta = null; static{ try { ACCESS_KEY_ID = SysConst.getProperty("OSS_ACCESS_KEY_ID"); ACCESS_KEY_SECRET = SysConst.getProperty("OSS_ACCESS_KEY_SECRET"); ENDPOINT = SysConst.getProperty("OSS_ENDPOINT"); BUCKETNAME = SysConst.getProperty("OSS_BUCKETNAME"); BUCKETNAME_VIDEO = SysConst.getProperty("OSS_BUCKETNAME_VIDEO"); OUT_OF_DATE = SysConst.getProperty("OSS_OUT_OF_DATE"); } catch (java.lang.Exception e) { e.printStackTrace(); } } public void init(){ // <a href="https://www.baidu.com/s?wd=%E5%88%9D%E5%A7%8B%E5%8C%96&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4P1R1myc4rH9bmHFBPjDz0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHcsPWD3nW0sP1TdPWm4PWD4" target="_blank" class="baidu-highlight">初始化</a>一个OSSClient client = new OSSClient(ENDPOINT,ACCESS_KEY_ID, ACCESS_KEY_SECRET); meta = new ObjectMetadata(); } /** * @Description: 上传文件到OSS<a href="https://www.baidu.com/s?wd=%E6%96%87%E4%BB%B6%E6%9C%8D%E5%8A%A1%E5%99%A8&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4P1R1myc4rH9bmHFBPjDz0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHcsPWD3nW0sP1TdPWm4PWD4" target="_blank" class="baidu-highlight">文件服务器</a> * @param content 文件流 * @param key 上传为OSS<a href="https://www.baidu.com/s?wd=%E6%96%87%E4%BB%B6%E6%9C%8D%E5%8A%A1%E5%99%A8&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4P1R1myc4rH9bmHFBPjDz0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHcsPWD3nW0sP1TdPWm4PWD4" target="_blank" class="baidu-highlight">文件服务器</a>的唯一标识 * @param mimeType 文档类型 * @throws Exception * @ReturnType:String OSSObject的ETag值。 */ public String uploadFile(InputStream content,String key,String mimeType) throws Exception{ //进行<a href="https://www.baidu.com/s?wd=%E5%88%9D%E5%A7%8B%E5%8C%96&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4P1R1myc4rH9bmHFBPjDz0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHcsPWD3nW0sP1TdPWm4PWD4" target="_blank" class="baidu-highlight">初始化</a> init(); // 必须设置ContentLength meta.setContentType(mimeType); // 上传Object. LogUtil.printConsoleLog("*****************上传图片到oss服务器开始*****************" + key); PutObjectResult result = client.putObject(BUCKETNAME, key, content, meta); LogUtil.printConsoleLog("*****************上传图片到oss服务器结束*****************" + key); return result.getETag(); } /** * @Description: 根据key获取oss服务器上的图片地址 * @param key * @return * @ReturnType:String */ public String getImgURl(String key){ init(); Date expires = new Date (new Date().getTime() + Integer.parseInt(OUT_OF_DATE)); // 30 minute to expire GeneratePresignedUrlRequest generatePresignedUrlRequest ; if(key.startsWith("msplatform")){ generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME, key); }else { generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME_VIDEO, key); } generatePresignedUrlRequest.setExpiration(expires); URL url = client.generatePresignedUrl(generatePresignedUrlRequest); return url.toString(); } /** * @Description:根据key获取oss服务器上的ipa文件地址 * @param key * @return * @ReturnType:String */ public String getIpaURl(String key){ init(); Date expires = new Date(new Date().getTime()+ 10*365*24*3600*1000); GeneratePresignedUrlRequest generatePresignedUrlRequest ; if(key.startsWith("msplatform")){ generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME, key); }else { generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME_VIDEO, key); } generatePresignedUrlRequest.setExpiration(expires); URL url = client.generatePresignedUrl(generatePresignedUrlRequest); return url.toString(); } /** * @Description: 根据key获取oss服务器上的图片地址 * @param key * @return * @ReturnType:String */ public InputStream getObject(String key){ init(); OSSObject object = null; if(key.startsWith("msplatform")){ object= client.getObject(BUCKETNAME, key); }else{ object= client.getObject(BUCKETNAME_VIDEO, key); } object.getObjectMetadata().getContentType(); // 获取Object的<a href="https://www.baidu.com/s?wd=%E8%BE%93%E5%85%A5%E6%B5%81&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4P1R1myc4rH9bmHFBPjDz0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHcsPWD3nW0sP1TdPWm4PWD4" target="_blank" class="baidu-highlight">输入流</a> InputStream objectContent = object.getObjectContent(); return objectContent; } /** * @Description:删除文件 * @param key OSS<a href="https://www.baidu.com/s?wd=%E6%96%87%E4%BB%B6%E6%9C%8D%E5%8A%A1%E5%99%A8&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4P1R1myc4rH9bmHFBPjDz0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHcsPWD3nW0sP1TdPWm4PWD4" target="_blank" class="baidu-highlight">文件服务器</a>上文件的唯一标识 * @ReturnType:void */ public void deleteFile(String key){ init(); client.deleteObject(BUCKETNAME, key); } /** * @Description: 断点上传文件到OSS文件服务器 * @param content 文件流 * @param key 上传为OSS文件服务器的唯一标识 * @param position 位置 */ public String appendObjectFile(InputStream content,String key,int position,String mimeType) throws Exception{ //进行<a href="https://www.baidu.com/s?wd=%E5%88%9D%E5%A7%8B%E5%8C%96&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4P1R1myc4rH9bmHFBPjDz0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHcsPWD3nW0sP1TdPWm4PWD4" target="_blank" class="baidu-highlight">初始化</a> init(); // 必须设置ContentLength meta.setContentLength(position); meta.setContentType(mimeType); meta.setCacheControl("no-cache"); meta.setContentEncoding("utf-8"); // 上传 LogUtil.printConsoleLog("*****************断点上传图片到oss服务器开始*****************" + key); AppendObjectRequest appendObjectRequest = new AppendObjectRequest(BUCKETNAME, key, content, meta); appendObjectRequest.setPosition(Long.valueOf(position)); AppendObjectResult appendObjectResult =client.appendObject(appendObjectRequest); LogUtil.printConsoleLog("*****************断点上传图片到oss服务器结束*****************" + key); return appendObjectResult.getNextPosition().toString(); } public static void main(String[] args) { OssManage ossManage = new OssManage(); } }

转载

 


__EOF__

本文作者皮军旗
本文链接https://www.cnblogs.com/pijunqi/p/14756359.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   皮军旗  阅读(113)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示