Search in sources :

Example 1 with BlobParallelUploadOptions

use of com.azure.storage.blob.options.BlobParallelUploadOptions in project vidflow-backend by CryptoSingh1337.

the class VideoOperationsServiceImpl method uploadVideo.

@Override
public List<String> uploadVideo(String username, MultipartFile videoFile) {
    if (validateVideoFileType(videoFile.getContentType())) {
        log.debug("Uploading video...");
        List<String> videoDetails = new ArrayList<>(2);
        String id = new ObjectId().toString();
        videoDetails.add(id);
        BlobClient blobClient = blobContainerClient.getBlobClient(generateUploadBlobName(username, id, getFileType(Objects.requireNonNull(videoFile.getOriginalFilename()))));
        try {
            BlobParallelUploadOptions blobParallelUploadOptions = new BlobParallelUploadOptions(videoFile.getInputStream());
            blobParallelUploadOptions.setHeaders(new BlobHttpHeaders().setContentType(videoFile.getContentType()));
            blobClient.uploadWithResponse(blobParallelUploadOptions, null, Context.NONE);
            videoDetails.add(blobClient.getBlockBlobClient().getBlobUrl());
            return videoDetails;
        } catch (IOException ignored) {
            throw new UploadFailedException("Video is unable to upload");
        }
    } else {
        throw new UnsupportedFormatException("Unsupported video format");
    }
}
Also used : BlobParallelUploadOptions(com.azure.storage.blob.options.BlobParallelUploadOptions) BlobHttpHeaders(com.azure.storage.blob.models.BlobHttpHeaders) BlobClient(com.azure.storage.blob.BlobClient) UnsupportedFormatException(com.saransh.vidflow.exceptions.UnsupportedFormatException) UploadFailedException(com.saransh.vidflow.exceptions.UploadFailedException) ObjectId(org.bson.types.ObjectId) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 2 with BlobParallelUploadOptions

use of com.azure.storage.blob.options.BlobParallelUploadOptions in project OpenSearch by opensearch-project.

the class AzureBlobStore method writeBlob.

public void writeBlob(String blobName, InputStream inputStream, long blobSize, boolean failIfAlreadyExists) throws URISyntaxException, BlobStorageException, IOException {
    assert inputStream.markSupported() : "Should not be used with non-mark supporting streams as their retry handling in the SDK is broken";
    logger.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {})", blobName, blobSize));
    final Tuple<BlobServiceClient, Supplier<Context>> client = client();
    final BlobContainerClient blobContainer = client.v1().getBlobContainerClient(container);
    final BlobClient blob = blobContainer.getBlobClient(blobName);
    try {
        final BlobRequestConditions blobRequestConditions = new BlobRequestConditions();
        if (failIfAlreadyExists) {
            blobRequestConditions.setIfNoneMatch(Constants.HeaderConstants.ETAG_WILDCARD);
        }
        SocketAccess.doPrivilegedVoidException(() -> {
            final Response<?> response = blob.uploadWithResponse(new BlobParallelUploadOptions(inputStream, blobSize).setRequestConditions(blobRequestConditions).setParallelTransferOptions(service.getBlobRequestOptionsForWriteBlob()), timeout(), client.v2().get());
            logger.trace(() -> new ParameterizedMessage("upload({}, stream, {}) - status [{}]", blobName, blobSize, response.getStatusCode()));
        });
    } catch (final BlobStorageException se) {
        if (failIfAlreadyExists && se.getStatusCode() == HttpURLConnection.HTTP_CONFLICT && BlobErrorCode.BLOB_ALREADY_EXISTS.equals(se.getErrorCode())) {
            throw new FileAlreadyExistsException(blobName, null, se.getMessage());
        }
        throw se;
    } catch (final RuntimeException ex) {
        // are swallowed and wrapped into runtime one (see please Exceptions.ReactiveException).
        if (ex.getCause() != null) {
            Throwables.rethrow(ex.getCause());
        } else {
            throw ex;
        }
    }
    logger.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {}) - done", blobName, blobSize));
}
Also used : BlobParallelUploadOptions(com.azure.storage.blob.options.BlobParallelUploadOptions) BlobContainerClient(com.azure.storage.blob.BlobContainerClient) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) BlobClient(com.azure.storage.blob.BlobClient) BlobServiceClient(com.azure.storage.blob.BlobServiceClient) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Supplier(java.util.function.Supplier) BlobRequestConditions(com.azure.storage.blob.models.BlobRequestConditions) BlobStorageException(com.azure.storage.blob.models.BlobStorageException)

Example 3 with BlobParallelUploadOptions

use of com.azure.storage.blob.options.BlobParallelUploadOptions in project vidflow-backend by CryptoSingh1337.

the class ThumbnailOperationsServiceImpl method uploadThumbnail.

@Override
public String uploadThumbnail(String username, String videoId, MultipartFile thumbnail) {
    if (validateImageFileType(thumbnail.getContentType())) {
        log.debug("Uploading thumbnail...");
        BlobClient blobClient = containerClient.getBlobClient(generateBlobName(username, videoId, getFileType(Objects.requireNonNull(thumbnail.getOriginalFilename()))));
        try {
            BlobParallelUploadOptions blobParallelUploadOptions = new BlobParallelUploadOptions(thumbnail.getInputStream());
            blobParallelUploadOptions.setHeaders(new BlobHttpHeaders().setContentType(thumbnail.getContentType()));
            blobClient.uploadWithResponse(blobParallelUploadOptions, null, Context.NONE);
            return blobClient.getBlockBlobClient().getBlobUrl();
        } catch (IOException e) {
            throw new UploadFailedException("Thumbnail is unable to upload");
        }
    } else {
        throw new UnsupportedFormatException("Unsupported thumbnail format");
    }
}
Also used : BlobParallelUploadOptions(com.azure.storage.blob.options.BlobParallelUploadOptions) BlobHttpHeaders(com.azure.storage.blob.models.BlobHttpHeaders) BlobClient(com.azure.storage.blob.BlobClient) UnsupportedFormatException(com.saransh.vidflow.exceptions.UnsupportedFormatException) UploadFailedException(com.saransh.vidflow.exceptions.UploadFailedException) IOException(java.io.IOException)

Aggregations

BlobClient (com.azure.storage.blob.BlobClient)3 BlobParallelUploadOptions (com.azure.storage.blob.options.BlobParallelUploadOptions)3 BlobHttpHeaders (com.azure.storage.blob.models.BlobHttpHeaders)2 UnsupportedFormatException (com.saransh.vidflow.exceptions.UnsupportedFormatException)2 UploadFailedException (com.saransh.vidflow.exceptions.UploadFailedException)2 IOException (java.io.IOException)2 BlobContainerClient (com.azure.storage.blob.BlobContainerClient)1 BlobServiceClient (com.azure.storage.blob.BlobServiceClient)1 BlobRequestConditions (com.azure.storage.blob.models.BlobRequestConditions)1 BlobStorageException (com.azure.storage.blob.models.BlobStorageException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 ArrayList (java.util.ArrayList)1 Supplier (java.util.function.Supplier)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 ObjectId (org.bson.types.ObjectId)1