Java integrates Alibaba Cloud OSS to implement file upload function
Alibaba Cloud OSS (Object Storage Service) is a simple, efficient, safe and reliable cloud storage service that provides massive, safe, low-cost, and high reliability cloud storage solutions. By using Alibaba Cloud OSS, we can easily store files in the cloud and realize file upload, download, management and other functions. This article will introduce how to use Java language to integrate Alibaba Cloud OSS to implement the file upload function.
<dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>2.9.3</version> </dependency>
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; public class OSSUploader { private static final String ENDPOINT = "https://oss-cn-xxx.aliyuncs.com"; private static final String ACCESS_KEY_ID = "your-access-key-id"; private static final String ACCESS_KEY_SECRET = "your-access-key-secret"; private static final String BUCKET_NAME = "your-bucket-name"; public static void main(String[] args) { // 创建OSSClient实例 OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET); // ... // 其他操作代码 // ... // 关闭OSSClient实例 ossClient.shutdown(); } }
Replace "your-access-key-id", "your-access-key-secret", and "your-bucket-name" in the above code with your own AccessKeyId, AccessKeySecret and Bucket name.
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.PutObjectRequest; import com.aliyun.oss.model.PutObjectResult; import java.io.File; public class OSSUploader { private static final String ENDPOINT = "https://oss-cn-xxx.aliyuncs.com"; private static final String ACCESS_KEY_ID = "your-access-key-id"; private static final String ACCESS_KEY_SECRET = "your-access-key-secret"; private static final String BUCKET_NAME = "your-bucket-name"; public static void main(String[] args) { // 创建OSSClient实例 OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET); // 上传文件 String fileKey = "example.jpg"; // 上传到OSS的文件名 String filePath = "path/to/example.jpg"; // 本地文件路径 PutObjectResult result = ossClient.putObject(new PutObjectRequest(BUCKET_NAME, fileKey, new File(filePath))); // 打印上传结果 System.out.println("ETag:" + result.getETag()); System.out.println("RequestId:" + result.getRequestId()); // 关闭OSSClient实例 ossClient.shutdown(); } }
Replace "example.jpg" in the above code with the name of the file you want to upload , replace "path/to/example.jpg" with the path to your local file.
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.PutObjectRequest; import com.aliyun.oss.model.PutObjectResult; import java.io.File; public class OSSUploader { private static final String ENDPOINT = "https://oss-cn-xxx.aliyuncs.com"; private static final String ACCESS_KEY_ID = "your-access-key-id"; private static final String ACCESS_KEY_SECRET = "your-access-key-secret"; private static final String BUCKET_NAME = "your-bucket-name"; private OSS ossClient; public OSSUploader() { this.ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET); } public void uploadFile(String fileKey, String filePath) { PutObjectResult result = ossClient.putObject(new PutObjectRequest(BUCKET_NAME, fileKey, new File(filePath))); System.out.println("ETag:" + result.getETag()); System.out.println("RequestId:" + result.getRequestId()); } public void shutdown() { ossClient.shutdown(); } }
In the above code, we place the creation and closing logic of the OSSClient instance in the constructor and shutdown method, and the uploadFile method is used to upload files.
The above are the steps and sample code for using Java language to integrate Alibaba Cloud OSS to implement the file upload function. By integrating OSS, we can easily upload files to the cloud with high reliability and security. Hope this article is helpful to you!
The above is the detailed content of Java integrates Alibaba Cloud OSS to implement file upload function. For more information, please follow other related articles on the PHP Chinese website!