develop #4
5
src/main/java/com/example/nto/service/PhotoService.java
Normal file
5
src/main/java/com/example/nto/service/PhotoService.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package com.example.nto.service;
|
||||||
|
|
||||||
|
public interface PhotoService {
|
||||||
|
String uploadProfilePhoto(long id, byte[] photo);
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
|
import com.amazonaws.services.s3.AmazonS3;
|
||||||
|
import com.amazonaws.services.s3.model.AmazonS3Exception;
|
||||||
|
import com.amazonaws.services.s3.model.ObjectMetadata;
|
||||||
|
import com.example.nto.aspect.annotation.LogExample;
|
||||||
|
import com.example.nto.config.ObjectStorageConfig;
|
||||||
|
import com.example.nto.service.PhotoService;
|
||||||
|
import com.example.nto.utils.Utils;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class PhotoServiceImpl implements PhotoService {
|
||||||
|
private final ObjectStorageConfig objectStorageConfig;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@LogExample
|
||||||
|
public String uploadProfilePhoto(long id, byte[] photo) {
|
||||||
|
final String url;
|
||||||
|
final String bucketName = objectStorageConfig.getBucketName();
|
||||||
|
final AmazonS3 s3Client = objectStorageConfig.createAmazonS3();
|
||||||
|
|
||||||
|
try {
|
||||||
|
String fileName = Utils.profileFileName(id);
|
||||||
|
ObjectMetadata metadata = new ObjectMetadata();
|
||||||
|
metadata.setContentLength(photo.length);
|
||||||
|
|
||||||
|
// Загрузка файла в Yandex Object Storage
|
||||||
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(photo);
|
||||||
|
s3Client.putObject(bucketName, fileName, inputStream, metadata);
|
||||||
|
|
||||||
|
// Получение ссылки на загруженный файл
|
||||||
|
url = s3Client.getUrl(bucketName, fileName).toExternalForm();
|
||||||
|
|
||||||
|
} catch (AmazonS3Exception e) {
|
||||||
|
throw new AmazonS3Exception(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user