Amazon AWS -Fehler mit S3 -Bucket nicht vorhanden (Federschuh)Java

Java-Forum
Guest
 Amazon AWS -Fehler mit S3 -Bucket nicht vorhanden (Federschuh)

Post by Guest »

Ich entwickle eine Produktregistrierungs-API im Spring-Boot und sende in meiner Post-Methode ein Bild-Upload über Multipart/Form-Data, zusammen mit einem JSON mit dem DTO meines Produkts. Wie Sie sehen können: < /p>

Code: Select all

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/product")
public class ProductController {
final ProductService productService;
final CategoryService categoryService;
final ProductMapper productMapper;
final S3Client s3Client;

private final String BUCKET_NAME = "awstockproducts" + System.currentTimeMillis();

public ProductController(ProductService productService, ProductMapper productMapper, CategoryService categoryService, S3Client s3Client) {
this.productService = productService;
this.productMapper = productMapper;
this.categoryService = categoryService;
this.s3Client = s3Client;
}
@PostMapping(value = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity saveProduct (@RequestParam("productDto") String jsonString, @RequestParam("file")MultipartFile file) {
try {
ObjectMapper objectMapper = new ObjectMapper();
ProductDto productDto = objectMapper.readValue(jsonString, ProductDto.class);

if (productService.existsByProduct(productDto.getProduct())) {
return ResponseEntity.status(HttpStatus.CONFLICT).body("Product already exists!");
}
ProductModel productModel = productMapper.toProductModel(productDto);
CategoryModel categoryModel = categoryService.findById(productDto.getProductCategory().getCategory_id())
.orElseThrow(() -> new RuntimeException("Category not found"));
productModel.setProductCategory(categoryModel);

String fileName = "/products/images/" + UUID.randomUUID().toString() + "-" + file.getOriginalFilename();

s3Client.putObject(PutObjectRequest
.builder()
.bucket(BUCKET_NAME)
.key(fileName)
.build(),
software.amazon.awssdk.core.sync.RequestBody.fromString("Testing java sdk"));
return ResponseEntity.status(HttpStatus.CREATED).body(productService.save(productModel));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.CONFLICT).body(e);
}
}

< /code>
Das Problem ist, dass ich Probleme habe, die Anfrage von meinem Postman zu machen < /p>
    "localizedMessage": "The specified bucket does not exist (Service: S3, Status Code: 404, Request ID: ZZ27F9CNNTG2RTPR, Extended Request ID:
< /code>
Trotz der Nachricht, überprüft Amazon AWS Ich kann sehen, dass mein Eimer tatsächlich existiert und denselben Namen hat wie der, den ich in meiner Anwendung konfiguriert habe. Dies ist AmazonConfig < /p>
package com.api.business_manager_api.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

@Configuration
public class AmazonConfig {
private final String AWS_REGION = "us-east-1";
private final String BUCKET_NAME = "productstockimages";

@Value("${aws.accessKeyId}")
private String accessKeyId;

@Value("${aws.secretAccessKey}")
private String secretAccessKey;

@Bean
public S3Client s3Client() {
AwsBasicCredentials awsCreds = AwsBasicCredentials.create(accessKeyId, secretAccessKey);
return S3Client.builder()
.region(Region.of(AWS_REGION))
.credentialsProvider(StaticCredentialsProvider.create(awsCreds))
.build();
}
}

In meinem SecretKey und in meinem AccessKey habe ich in Eigenschaften mit den Schlüssel meines autorisierten IAM -Benutzers von der Amazon AWS -Konsole konfiguriert.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post