Skip to content

Unable to upload files using AWS SDK #225

@Kingyumu

Description

@Kingyumu

It has been resolved because the SDK version is not supported.
The available version is:

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>s3</artifactId>
    <version>2.25.27</version>
</dependency>

I used Docker Compose to deploy RustFS

services:
  rustfs:
    image: rustfs/rustfs:latest
    container_name: rustfs
    ports:
      - "9000:9000"
    volumes:
      - "./data:/data"
    command: [
      "--address", ":9000",
      "--console-enable",
      "--access-key", "admin",
      "--secret-key", "10086110",
      "/data"
    ]
    restart: always

After deployment, the management page can be opened normally on the web side, and files can be uploaded on the page.

Image

I created a user on the page, set up an access account, and configured policies

Image Image

My demo situation:

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>s3</artifactId>
    <version>2.31.77</version>
</dependency>
@Data
@Configuration
@ConfigurationProperties(prefix = "rustfs")
public class AwsS3Config {

    private String endpoint;

    private String accessKey;

    private String secretKey;

    private String bucketName;

    @Bean
    public S3Client s3Client() {
        return S3Client.builder()
                .endpointOverride(URI.create(endpoint))
                .credentialsProvider(StaticCredentialsProvider.create(
                        AwsBasicCredentials.create(accessKey, secretKey)
                ))
                .region(Region.US_EAST_1)
                .forcePathStyle(true)
                .build();
    }

}

application.yml

rustfs:
  # 服务器地址
  endpoint: ${RUSTFS_ENDPOINT:http://my-ip:9000}
  # 访问密钥
  access-key: ${RUSTFS_ACCESS_KEY:EPMLKxxxxdnceqoO2ja}
  # 私钥
  secret-key: ${RUSTFS_SECRET_KEY:E3veIjVFPw9xxxxsmU02ODqhL8TNSonWKYZ5H4}
  # 默认存储桶
  bucket-name: ${RUSTFS_BUCKET_NAME:cup}

S3FileService

/**
     * 上传文件流到S3
     *
     * @param key         S3对象键
     * @param bytes 二进制文件
     * @param contentType 文件类型
     * @return 上传结果
     */
    public PutObjectResponse uploadFile(String key, byte[] bytes, String contentType) {
        try {
            PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                    .bucket(bucketName)
                    .key(key)
                    .contentType(contentType)
                    .build();

            return s3Client.putObject(putObjectRequest, RequestBody.fromBytes(bytes));
        } catch (S3Exception e) {
            log.error("Failed to upload file stream to S3. Key: {}, Error: {}", key, e.getMessage(), e);
            throw new S3OperationException("Failed to upload file stream to S3", e);
        }
    }

controller

@RestController
@RequestMapping("/rustfs")
public class RustfsController {

    @Autowired
    private S3FileService s3FileService;

    /**
     * 上传文件到S3
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        s3FileService.uploadFile(file.getOriginalFilename(), file.getBytes(), file.getContentType());
        return "upload";
    }

}

Interface call log

org.apache.http.NoHttpResponseException: The target server failed to respond

See the attachment for detailed logs

log.txt

Metadata

Metadata

Assignees

Labels

S-resolvingStatus: Resolving a issuebugSomething isn't workingsdk-javaJava SDK related issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions