首页 > 代码库 > 上传图片到阿里云oss

上传图片到阿里云oss

阿里云地址

登录阿里云管理控制台,创建对象存储oss

private static final String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
private static final String accessKeyId = PropKit.get("accessKeyId");
private static final String accessKeySecret = PropKit.get("accessKeySecret");
private static final String bucket = PropKit.get("bucket");
private static final String allEndPoint = PropKit.get("allEndPoint");//oss bucket全路径



public static String uploadImage(UploadFile upFile){ String dir = "image/temp/"; String forder = UUID.randomUUID().toString(); File file = upFile.getFile(); String fileName = file.getName(); // 创建OSSClient实例 OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); // 上传文件 PutObjectResult result = ossClient.putObject(bucket, dir+forder+"/"+fileName, file); // 关闭client ossClient.shutdown(); //删除存在本地的图片 file.delete(); //返回图片访问url(打开对象存储访问权限,直接拼接链接就可以访问上传的图片) return allEndPoint+dir+forder+"/"+URLEncoder.encode(fileName); } }

 

上传图片到阿里云oss