阿里云 对象存储java实现OSS文件上传及获取URL

本人已尝试上传成功

maven加载的jar包 pom配置


    com.aliyun.oss
    aliyun-sdk-oss
    2.8.3

java代码文件上传及返回访问的URL
public static String picOSS( MultipartFile uploadFile) throws Exception {
		String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
		// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录
		// https://ram.console.aliyun.com 创建
		String accessKeyId = "";
		String accessKeySecret = "";
		// 创建OSSClient实例
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		// 上传
		long time = new Date().getTime();
	        ossClient.putObject("bucketName", "filename", new ByteArrayInputStream(uploadFile.getBytes()));
		// 关闭client
		ossClient.shutdown();
		Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
		String url = ossClient.generatePresignedUrl("bucketName", "filename", expiration).toString();
		return url;
	}

你可能感兴趣的:(文件上传)