将 MultipartFile 上传到 Oracle ObjectStorage

Upload a MultipartFile to a Oracle ObjectStorage

我有一个接受 MultipartFile 列表的 Rest 点,如下所示:

@CrossOrigin(origins = "*", methods = {RequestMethod.POST})
@PostMapping(value = "/files", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
ResponseEntity<UploadFileResponse> uploadDocuments(@RequestPart("files") List<MultipartFile> files) throws InfoNotFoundException, IOException {
    log.info("Upload Documents controller");
    if (files == null || files.isEmpty()) {
        throw new RuntimeException("You must select at least one file for uploading");
    }

    UploadFileResponse uploadFileResponse = service.uploadFiles(files);

    ResponseEntity<UploadFileResponse> response = new ResponseEntity<UploadFileResponse>(uploadFileResponse, HttpStatus.OK);
    return response;
}

这是在调用一个服务,该服务在内部调用了一个方法来将信息保存在来自 oracle 的 ObjectStorage 中,如下所示:

public void upload(MultipartFile file) throws Exception {

    HSAClientConfiguration conf = new HSAClientConfiguration();

    UploadObject app = new UploadObject();

    String fileName = "oci/oci_api_key.pem";
    InputStream is = app.getFileFromResourceAsStream(fileName);

    String result = IOUtils.toString(is, StandardCharsets.UTF_8);

    log.info("Authenticating...");
    AuthenticationDetailsProvider authenticationDetailsProvider =
            SimpleAuthenticationDetailsProvider.builder()
                    .tenantId(conf.getTenantId())
                    .userId(conf.getUserId())
                    .fingerprint(conf.getFingerprint())
                    .privateKeySupplier(new StringPrivateKeySupplier(result))
                    .build();

    ObjectStorage client = new ObjectStorageClient(authenticationDetailsProvider);
    client.setRegion(Region.EU_FRANKFURT_1);

    //Construccion del nombre del archivo.
    String fecha = DateUtil.convertToYYYYMM(Timestamp.from(Instant.now()));
    String objectName = fecha + "/" + file.getOriginalFilename();
    log.info("Loading the file to Object Storage with name: " + objectName);

    //Convertir el fichero para pasar en el putObject
    InputStream inputStream = file.getInputStream();


    log.info("Creating the source object to send");
    PutObjectRequest putObjectRequest =
            PutObjectRequest.builder()
                    .namespaceName(conf.getNameSpace())
                    .bucketName(conf.getBucket())
                    .objectName(objectName)
                    .contentLength(file.getSize())
                    .putObjectBody(inputStream)
                    .build();
    client.putObject(putObjectRequest);

    PutObjectResponse putObjectResponse = client.putObject(putObjectRequest);
    System.out.println("Response: " + putObjectRequest);

}

如果我将系统中文件的输入流传递给此代码而不是 Multipart,它将被正确保存。但是当我使用 MultipartFile 时,我从 RestClient 收到这样的错误:

com.oracle.bmc.http.internal.RestClient  : Error calling available on the stream to get the available number of bytes

看起来这是在尝试序列化对象的 oracle 类中产生的。我不知道为什么。有谁知道如何序列化 InputStream 或处理这个?

谢谢

尝试序列化 InputStream 的响应是一个愚蠢的问题。如果你删除 PutObjectResponse putObjectResponse = client.putObject(putObjectRequest); 代码工作顺利。

尽情享受吧!