|
@@ -0,0 +1,74 @@
|
|
|
|
|
+package com.handler.controller;
|
|
|
|
|
+
|
|
|
|
|
+import com.handler.entity.RecordInfo;
|
|
|
|
|
+import com.volcengine.tos.TOSV2;
|
|
|
|
|
+import com.volcengine.tos.TOSV2ClientBuilder;
|
|
|
|
|
+import com.volcengine.tos.TosClientException;
|
|
|
|
|
+import com.volcengine.tos.TosServerException;
|
|
|
|
|
+import com.volcengine.tos.model.object.PutObjectInput;
|
|
|
|
|
+import com.volcengine.tos.model.object.PutObjectOutput;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 角色监控关联Controller
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author jy
|
|
|
|
|
+ * @date 2023-09-19
|
|
|
|
|
+ */
|
|
|
|
|
+@RestController
|
|
|
|
|
+@RequestMapping("recordMigrate")
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class RecordHandlerController {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public void PutObjectWithFileInputStreamExample(RecordInfo recordInfo) {
|
|
|
|
|
+ String endpoint = "your endpoint";
|
|
|
|
|
+ String region = "your region";
|
|
|
|
|
+ String accessKey = System.getenv("TOS_ACCESS_KEY");
|
|
|
|
|
+ String secretKey = System.getenv("TOS_SECRET_KEY");
|
|
|
|
|
+
|
|
|
|
|
+ String bucketName = "bucket-example";
|
|
|
|
|
+ // 对象名,模拟 example_dir 下的 example_object.txt 文件
|
|
|
|
|
+ String objectKey = "example_dir/example_object.txt";
|
|
|
|
|
+ // 本地文件路径,请保证文件存在,暂不支持文件夹功能
|
|
|
|
|
+ String filePath = "example_dir/example_file.txt";
|
|
|
|
|
+
|
|
|
|
|
+ TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
|
|
|
|
|
+
|
|
|
|
|
+ File file = new File(filePath);
|
|
|
|
|
+ try(FileInputStream inputStream = new FileInputStream(file)){
|
|
|
|
|
+ PutObjectInput putObjectInput = new PutObjectInput().setBucket(bucketName)
|
|
|
|
|
+ .setKey(objectKey).setContent(inputStream).setContentLength(file.length());
|
|
|
|
|
+ PutObjectOutput output = tos.putObject(putObjectInput);
|
|
|
|
|
+ System.out.println("putObject succeed, object's etag is " + output.getEtag());
|
|
|
|
|
+ System.out.println("putObject succeed, object's crc64 is " + output.getHashCrc64ecma());
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ System.out.println("putObject read file failed");
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } catch (TosClientException e) {
|
|
|
|
|
+ // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
|
|
|
|
|
+ System.out.println("putObject failed");
|
|
|
|
|
+ System.out.println("Message: " + e.getMessage());
|
|
|
|
|
+ if (e.getCause() != null) {
|
|
|
|
|
+ e.getCause().printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (TosServerException e) {
|
|
|
|
|
+ // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
|
|
|
|
|
+ System.out.println("putObject failed");
|
|
|
|
|
+ System.out.println("StatusCode: " + e.getStatusCode());
|
|
|
|
|
+ System.out.println("Code: " + e.getCode());
|
|
|
|
|
+ System.out.println("Message: " + e.getMessage());
|
|
|
|
|
+ System.out.println("RequestID: " + e.getRequestID());
|
|
|
|
|
+ } catch (Throwable t) {
|
|
|
|
|
+ // 作为兜底捕获其他异常,一般不会执行到这里
|
|
|
|
|
+ System.out.println("putObject failed");
|
|
|
|
|
+ System.out.println("unexpected exception, message: " + t.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|