java 上传zip文件到s3
时间: 2024-01-07 12:04:46 浏览: 193
您可以使用 AWS SDK for Java 来上传文件到 AWS S3。下面是一个示例代码:
```java
import java.io.File;
import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;
public class S3Uploader {
private static String bucketName = "your-bucket-name";
private static String keyName = "your-object-key-name";
private static String uploadFilePath = "your-zip-file-path";
public static void main(String[] args) throws Exception {
AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
try {
System.out.println("Uploading a new object to S3 from a file\n");
File file = new File(uploadFilePath);
s3client.putObject(new PutObjectRequest(
bucketName, keyName, file));
} catch (AmazonClientException e) {
e.printStackTrace();
}
}
}
```
请确保您已经正确配置 AWS SDK for Java 并且已经授权了上传到指定 S3 Bucket 的权限。同时,还需要替换示例代码中的 `bucketName`,`keyName` 和 `uploadFilePath` 为您自己的值。
阅读全文