|
40 | 40 | import com.azure.storage.blob.models.StorageAccountInfo;
|
41 | 41 | import com.azure.storage.blob.models.TaggedBlobItem;
|
42 | 42 | import com.azure.storage.blob.models.UserDelegationKey;
|
| 43 | +import com.azure.storage.blob.options.BlobContainerCreateOptions; |
43 | 44 | import com.azure.storage.blob.options.FindBlobsOptions;
|
44 | 45 | import com.azure.storage.blob.sas.BlobServiceSasSignatureValues;
|
45 | 46 | import com.azure.storage.common.StorageSharedKeyCredential;
|
@@ -428,6 +429,83 @@ Mono<Response<Void>> createWithResponse(Map<String, String> metadata, PublicAcce
|
428 | 429 | .map(response -> new SimpleResponse<>(response, null));
|
429 | 430 | }
|
430 | 431 |
|
| 432 | + /** |
| 433 | + * Creates a new container within a storage account if it does not exist. For more information, see the |
| 434 | + * <a href="https://docs.microsoft.com/rest/api/storageservices/create-container">Azure Docs</a>. |
| 435 | + * |
| 436 | + * <p><strong>Code Samples</strong></p> |
| 437 | + * |
| 438 | + * <!-- src_embed com.azure.storage.blob.BlobContainerAsyncClient.createIfNotExists --> |
| 439 | + * <pre> |
| 440 | + * client.createIfNotExists().subscribe(created -> { |
| 441 | + * if (created) { |
| 442 | + * System.out.println("successfully created."); |
| 443 | + * } else { |
| 444 | + * System.out.println("Already exists."); |
| 445 | + * } |
| 446 | + * }); |
| 447 | + * </pre> |
| 448 | + * <!-- end com.azure.storage.blob.BlobContainerAsyncClient.createIfNotExists --> |
| 449 | + * |
| 450 | + * @return A reactive response signaling completion. {@code true} indicates a new container was created, |
| 451 | + * {@code true} indicates a container already existed at this location. |
| 452 | + */ |
| 453 | + @ServiceMethod(returns = ReturnType.SINGLE) |
| 454 | + public Mono<Boolean> createIfNotExists() { |
| 455 | + return createIfNotExistsWithResponse(null).map(response -> response.getStatusCode() != 409); |
| 456 | + } |
| 457 | + |
| 458 | + /** |
| 459 | + * Creates a new container within a storage account if it does not exist. For more information, see the |
| 460 | + * <a href="https://docs.microsoft.com/rest/api/storageservices/create-container">Azure Docs</a>. |
| 461 | + * |
| 462 | + * <p><strong>Code Samples</strong></p> |
| 463 | + * |
| 464 | + * <!-- src_embed com.azure.storage.blob.BlobContainerAsyncClient.createIfNotExistsWithResponse#Map-PublicAccessType --> |
| 465 | + * <pre> |
| 466 | + * Map<String, String> metadata = Collections.singletonMap("metadata", "value"); |
| 467 | + * BlobContainerCreateOptions options = new BlobContainerCreateOptions().setMetadata(metadata) |
| 468 | + * .setPublicAccessType(PublicAccessType.CONTAINER); |
| 469 | + * |
| 470 | + * client.createIfNotExistsWithResponse(options).subscribe(response -> { |
| 471 | + * if (response.getStatusCode() == 409) { |
| 472 | + * System.out.println("Already exists."); |
| 473 | + * } else { |
| 474 | + * System.out.println("successfully created."); |
| 475 | + * } |
| 476 | + * }); |
| 477 | + * </pre> |
| 478 | + * <!-- end com.azure.storage.blob.BlobContainerAsyncClient.createIfNotExistsWithResponse#Map-PublicAccessType --> |
| 479 | + * |
| 480 | + * @param options {@link BlobContainerCreateOptions} |
| 481 | + * @return A reactive response signaling completion. If {@link Response}'s status code is 201, a new container was |
| 482 | + * successfully created. If status code is 409, a container already existed at this location. |
| 483 | + */ |
| 484 | + @ServiceMethod(returns = ReturnType.SINGLE) |
| 485 | + public Mono<Response<Void>> createIfNotExistsWithResponse(BlobContainerCreateOptions options) { |
| 486 | + try { |
| 487 | + return createIfNotExistsWithResponse(options, null); |
| 488 | + } catch (RuntimeException ex) { |
| 489 | + return monoError(LOGGER, ex); |
| 490 | + } |
| 491 | + } |
| 492 | + |
| 493 | + Mono<Response<Void>> createIfNotExistsWithResponse(BlobContainerCreateOptions options, Context context) { |
| 494 | + try { |
| 495 | + options = options == null ? new BlobContainerCreateOptions() : options; |
| 496 | + return createWithResponse(options.getMetadata(), options.getPublicAccessType(), context) |
| 497 | + .onErrorResume(t -> t instanceof BlobStorageException && ((BlobStorageException) t) |
| 498 | + .getStatusCode() == 409, |
| 499 | + t -> { |
| 500 | + HttpResponse response = ((BlobStorageException) t).getResponse(); |
| 501 | + return Mono.just(new SimpleResponse<>(response.getRequest(), response.getStatusCode(), |
| 502 | + response.getHeaders(), null)); |
| 503 | + }); |
| 504 | + } catch (RuntimeException ex) { |
| 505 | + return monoError(LOGGER, ex); |
| 506 | + } |
| 507 | + } |
| 508 | + |
431 | 509 | /**
|
432 | 510 | * Marks the specified container for deletion. The container and any blobs contained within it are later deleted
|
433 | 511 | * during garbage collection. For more information, see the
|
@@ -500,6 +578,86 @@ Mono<Response<Void>> deleteWithResponse(BlobRequestConditions requestConditions,
|
500 | 578 | .map(response -> new SimpleResponse<>(response, null));
|
501 | 579 | }
|
502 | 580 |
|
| 581 | + /** |
| 582 | + * Marks the specified container for deletion if it exists. The container and any blobs contained within it are later deleted |
| 583 | + * during garbage collection. For more information, see the |
| 584 | + * <a href="https://docs.microsoft.com/rest/api/storageservices/delete-container">Azure Docs</a>. |
| 585 | + * |
| 586 | + * <p><strong>Code Samples</strong></p> |
| 587 | + * |
| 588 | + * <!-- src_embed com.azure.storage.blob.BlobContainerAsyncClient.deleteIfExists --> |
| 589 | + * <pre> |
| 590 | + * client.deleteIfExists().subscribe(deleted -> { |
| 591 | + * if (deleted) { |
| 592 | + * System.out.println("Successfully deleted."); |
| 593 | + * } else { |
| 594 | + * System.out.println("Does not exist."); |
| 595 | + * } |
| 596 | + * }); |
| 597 | + * </pre> |
| 598 | + * <!-- end com.azure.storage.blob.BlobContainerAsyncClient.deleteIfExists --> |
| 599 | + * |
| 600 | + * @return A reactive response signaling completion. {@code true} indicates the container was deleted, |
| 601 | + * {@code false} indicates the container does not exist. |
| 602 | + */ |
| 603 | + @ServiceMethod(returns = ReturnType.SINGLE) |
| 604 | + public Mono<Boolean> deleteIfExists() { |
| 605 | + return deleteIfExistsWithResponse(null).map(response -> response.getStatusCode() != 404); |
| 606 | + } |
| 607 | + |
| 608 | + /** |
| 609 | + * Marks the specified container for deletion if it exists. The container and any blobs contained within it are |
| 610 | + * later deleted during garbage collection. For more information, see the |
| 611 | + * <a href="https://docs.microsoft.com/rest/api/storageservices/delete-container">Azure Docs</a>. |
| 612 | + * |
| 613 | + * <p><strong>Code Samples</strong></p> |
| 614 | + * |
| 615 | + * <!-- src_embed com.azure.storage.blob.BlobContainerAsyncClient.deleteIfExistsWithResponse#BlobRequestConditions --> |
| 616 | + * <pre> |
| 617 | + * BlobRequestConditions requestConditions = new BlobRequestConditions() |
| 618 | + * .setLeaseId(leaseId) |
| 619 | + * .setIfUnmodifiedSince(OffsetDateTime.now().minusDays(3)); |
| 620 | + * |
| 621 | + * client.deleteIfExistsWithResponse(requestConditions).subscribe(response -> { |
| 622 | + * if (response.getStatusCode() == 404) { |
| 623 | + * System.out.println("Does not exist."); |
| 624 | + * } else { |
| 625 | + * System.out.println("successfully deleted."); |
| 626 | + * } |
| 627 | + * }); |
| 628 | + * </pre> |
| 629 | + * <!-- end com.azure.storage.blob.BlobContainerAsyncClient.deleteIfExistsWithResponse#BlobRequestConditions --> |
| 630 | + * |
| 631 | + * @param requestConditions {@link BlobRequestConditions} |
| 632 | + * @return A reactive response signaling completion. If {@link Response}'s status code is 202, the container was |
| 633 | + * successfully deleted. If status code is 404, the container does not exist. |
| 634 | + * @throws UnsupportedOperationException If either {@link BlobRequestConditions#getIfMatch()} or |
| 635 | + * {@link BlobRequestConditions#getIfNoneMatch()} is set. |
| 636 | + */ |
| 637 | + @ServiceMethod(returns = ReturnType.SINGLE) |
| 638 | + public Mono<Response<Void>> deleteIfExistsWithResponse(BlobRequestConditions requestConditions) { |
| 639 | + try { |
| 640 | + return deleteIfExistsWithResponse(requestConditions, null); |
| 641 | + } catch (RuntimeException ex) { |
| 642 | + return monoError(LOGGER, ex); |
| 643 | + } |
| 644 | + } |
| 645 | + |
| 646 | + Mono<Response<Void>> deleteIfExistsWithResponse(BlobRequestConditions requestConditions, Context context) { |
| 647 | + requestConditions = requestConditions == null ? new BlobRequestConditions() : requestConditions; |
| 648 | + try { |
| 649 | + return deleteWithResponse(requestConditions, context) |
| 650 | + .onErrorResume(t -> t instanceof BlobStorageException && ((BlobStorageException) t).getStatusCode() == 404, |
| 651 | + t -> { |
| 652 | + HttpResponse response = ((BlobStorageException) t).getResponse(); |
| 653 | + return Mono.just(new SimpleResponse<>(response.getRequest(), response.getStatusCode(), |
| 654 | + response.getHeaders(), null)); |
| 655 | + }); |
| 656 | + } catch (RuntimeException ex) { |
| 657 | + return monoError(LOGGER, ex); |
| 658 | + } |
| 659 | + } |
| 660 | + |
503 | 661 | /**
|
504 | 662 | * Returns the container's metadata and system properties. For more information, see the
|
505 | 663 | * <a href="https://docs.microsoft.com/rest/api/storageservices/get-container-metadata">Azure Docs</a>.
|
|
0 commit comments