| | |
| | | import com.amazonaws.client.builder.AwsClientBuilder; |
| | | import com.amazonaws.services.s3.AmazonS3; |
| | | import com.amazonaws.services.s3.AmazonS3Client; |
| | | import com.amazonaws.services.s3.model.CannedAccessControlList; |
| | | import com.amazonaws.services.s3.model.CreateBucketRequest; |
| | | import com.amazonaws.services.s3.model.ObjectMetadata; |
| | | import com.amazonaws.services.s3.model.PutObjectRequest; |
| | | import com.amazonaws.services.s3.model.*; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.oss.constant.OssConstant; |
| | |
| | | .withClientConfiguration(clientConfig) |
| | | .withCredentials(credentialsProvider) |
| | | .disableChunkedEncoding() |
| | | // https限制使用域名访问 需要此配置 站点填域名 |
| | | .enablePathStyleAccess() |
| | | .build(); |
| | | |
| | | createBucket(); |
| | |
| | | ObjectMetadata metadata = new ObjectMetadata(); |
| | | metadata.setContentType(contentType); |
| | | metadata.setContentLength(inputStream.available()); |
| | | client.putObject(new PutObjectRequest(properties.getBucketName(), path, inputStream, metadata)); |
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(properties.getBucketName(), path, inputStream, metadata); |
| | | // 设置上传对象的 Acl 为公共读 |
| | | putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead); |
| | | client.putObject(putObjectRequest); |
| | | } catch (Exception e) { |
| | | throw new OssException("上传文件失败,请检查配置信息:[" + e.getMessage() + "]"); |
| | | } |
| | |
| | | return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType); |
| | | } |
| | | |
| | | /** |
| | | * 获取文件元数据 |
| | | * |
| | | * @param path 完整文件路径 |
| | | */ |
| | | public ObjectMetadata getObjectMetadata(String path) { |
| | | S3Object object = client.getObject(properties.getBucketName(), path); |
| | | return object.getObjectMetadata(); |
| | | } |
| | | |
| | | public String getUrl() { |
| | | String domain = properties.getDomain(); |
| | | String endpoint = properties.getEndpoint(); |
| | |
| | | // 云服务商直接返回 |
| | | if (StringUtils.containsAny(endpoint, OssConstant.CLOUD_SERVICE)){ |
| | | if (StringUtils.isNotBlank(domain)) { |
| | | return domain; |
| | | return header + domain; |
| | | } |
| | | return header + properties.getBucketName() + "." + endpoint; |
| | | } |
| | | // minio 单独处理 |
| | | if (StringUtils.isNotBlank(domain)) { |
| | | return domain + "/" + properties.getBucketName(); |
| | | return header + domain + "/" + properties.getBucketName(); |
| | | } |
| | | return header + endpoint + "/" + properties.getBucketName(); |
| | | } |