File was renamed from ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/util/StateCloudObsUtil.java |
| | |
| | | package com.ruoyi.system.api.util; |
| | | package com.ruoyi.file.utils; |
| | | |
| | | import com.amazonaws.ClientConfiguration; |
| | | import com.amazonaws.Protocol; |
| | |
| | | public static String END_POINT = "obs.cn-sccd1.ctyun.cn"; |
| | | public static String BUCKET = "jyzx-obs"; |
| | | public static String DOMAIN = "https://" + BUCKET + "." + END_POINT + "/"; |
| | | |
| | | public static AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, |
| | | SECRET_KEY); |
| | | |
| | | public static String uploadFile(MultipartFile file) { |
| | | String originalFilename = file.getOriginalFilename(); |
| | |
| | | String fileName; |
| | | AmazonS3 s3client = null; |
| | | try { |
| | | AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, |
| | | SECRET_KEY); |
| | | ClientConfiguration awsClientConfig = new ClientConfiguration(); |
| | | awsClientConfig.setSignerOverride("AWSS3V4SignerType"); |
| | | awsClientConfig.setProtocol(Protocol.HTTP); |
| | |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static String obsUploadStream(String code, InputStream content) throws IOException { |
| | | String fileName = ""; |
| | | ObjectMetadata objectMetadata = new ObjectMetadata();// 创建上传Object的Metadata |
| | | fileName = "qrCode/" + UUID.randomUUID().toString().replaceAll("-", "") + "-id" + code |
| | | + ".png"; |
| | | AmazonS3 s3client = null; |
| | | try { |
| | | ClientConfiguration awsClientConfig = new ClientConfiguration(); |
| | | awsClientConfig.setSignerOverride("AWSS3V4SignerType"); |
| | | awsClientConfig.setProtocol(Protocol.HTTP); |
| | | s3client = AmazonS3ClientBuilder.standard() |
| | | .withCredentials(new AWSStaticCredentialsProvider(credentials)) |
| | | .withClientConfiguration(awsClientConfig) |
| | | .withEndpointConfiguration(new EndpointConfiguration(END_POINT, "")) |
| | | .disableChunkedEncoding() |
| | | .enablePathStyleAccess() |
| | | .build(); |
| | | System.out.print("=====connect success=====\n"); |
| | | // 上传 object |
| | | PutObjectRequest request = new PutObjectRequest(BUCKET, fileName, content, |
| | | objectMetadata); |
| | | PutObjectResult result = s3client.putObject(request); |
| | | System.out.format("Etag: %s, versionId: %s\n", result.getETag(), |
| | | result.getVersionId()); |
| | | System.out.print("=====request success=====\n"); |
| | | return DOMAIN + fileName; |
| | | } catch (Exception e) { |
| | | System.out.print("=====request fail=====\n"); |
| | | System.out.print(e.getMessage()); |
| | | } |
| | | return fileName; |
| | | } |
| | | } |