threeperson
发布于 2023-12-13 / 0 阅读
0
0

cloudflare r2上传

cloudflare r2文件上传工具类整合到springboot

###s3 sdk maven 依赖

```

<!--aws java sdk-->

<dependency>

<groupId>com.amazonaws</groupId>

<artifactId>aws-java-sdk-s3</artifactId>

<version>1.11.300</version>

</dependency>

<!--jcodec-->

```

###工具类R2Client

```

import com.amazonaws.auth.AWSStaticCredentialsProvider;

import com.amazonaws.auth.BasicAWSCredentials;

import com.amazonaws.client.builder.AwsClientBuilder;

import com.amazonaws.services.s3.AmazonS3;

import com.amazonaws.services.s3.AmazonS3ClientBuilder;

import com.amazonaws.services.s3.model.DeleteObjectRequest;

import com.amazonaws.services.s3.model.GetObjectRequest;

import com.amazonaws.services.s3.model.ObjectMetadata;

import com.amazonaws.services.s3.model.PutObjectRequest;

import com.amazonaws.services.s3.model.S3Object;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.nio.file.Files;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class R2Client {

private static final Logger logger = LoggerFactory.getLogger(R2Client.class);

private final AwsClientBuilder.EndpointConfiguration endpointConfiguration;

private final AWSStaticCredentialsProvider awsStaticCredentialsProvider;

private AmazonS3 client;

private R2Client(String apiUrl, String accessKey, String secretKey) {

this.endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(apiUrl, "auto");

this.awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey));

this.initialize();

}

public void initialize() {

this.client = (AmazonS3)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)AmazonS3ClientBuilder.standard().withEndpointConfiguration(this.endpointConfiguration)).withCredentials(this.awsStaticCredentialsProvider)).build();

}

public void deleteFile(String identifier, String bucket) {

this.client.deleteObject(new DeleteObjectRequest(bucket, identifier));

}

public void uploadFile(String identifier, String bucket, File file) {

this.client.putObject(new PutObjectRequest(bucket, identifier, file));

}

public void downloadFile(String identifier, String bucket, File file) {

S3Object object = this.client.getObject(new GetObjectRequest(bucket, identifier));

try {

InputStream inputStream = object.getObjectContent();

try {

OutputStream outputStream = Files.newOutputStream(file.toPath());

try {

byte[] buffer = new byte[1024];

int bytesRead;

while((bytesRead = inputStream.read(buffer)) > 0) {

outputStream.write(buffer, 0, bytesRead);

}

} catch (Throwable var11) {

if (outputStream != null) {

try {

outputStream.close();

} catch (Throwable var10) {

var11.addSuppressed(var10);

}

}

throw var11;

}

if (outputStream != null) {

outputStream.close();

}

} catch (Throwable var12) {

if (inputStream != null) {

try {

inputStream.close();

} catch (Throwable var9) {

var12.addSuppressed(var9);

}

}

throw var12;

}

if (inputStream != null) {

inputStream.close();

}

} catch (IOException var13) {

logger.error("downloadFile error", var13);

}

}

public ObjectMetadata getMetadata(String identifier, String bucket) {

return this.client.getObject(new GetObjectRequest(bucket, identifier)).getObjectMetadata();

}

public static R2Client create(String apiUrl, String accessKey, String secretKey) {

return new R2Client(apiUrl, accessKey, secretKey);

}

public static void main(String[] args) {

R2Client r2StorageAPI = create("https://{accountId}.r2.cloudflarestorage.com", {accessKey}, {secretKey});

r2StorageAPI.uploadFile("test/aa.mp4", "bucket", new File("C:\\Users\\Administrator\\Desktop\\aa.txt"));

}

}

```

###R2Client config

```

@Configuration

@ConfigurationProperties(prefix = "system.r2")

@Data

public class R2Config {

private String domain;

private String accountId;

private String accessKey;

private String secretKey;

@Bean

public R2Client getR2Client() {

String url = StringUtils.format("https://{}.r2.cloudflarestorage.com",accountId);

return R2Client.create(url, accessKey, secretKey);

}

}

```


评论