Skip to content

Commit

Permalink
Added ability to check if share, file or directory exists (#8477)
Browse files Browse the repository at this point in the history
  • Loading branch information
gapra-msft authored Mar 2, 2020
1 parent b15ef59 commit caec48f
Show file tree
Hide file tree
Showing 25 changed files with 891 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,28 +205,28 @@ public final BlobInputStream openInputStream(BlobRange range, BlobRequestConditi
}

/**
* Gets if the container this client represents exists in the cloud.
* Gets if the blob this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.blob.specialized.BlobClientBase.exists}
*
* @return true if the container exists, false if it doesn't
* @return true if the blob exists, false if it doesn't
*/
public Boolean exists() {
return existsWithResponse(null, Context.NONE).getValue();
}

/**
* Gets if the container this client represents exists in the cloud.
* Gets if the blob this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.blob.specialized.BlobClientBase.existsWithResponse#Duration-Context}
*
* @param timeout An optional timeout value beyond which a {@link RuntimeException} will be raised.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @return true if the container exists, false if it doesn't
* @return true if the blob exists, false if it doesn't
*/
public Response<Boolean> existsWithResponse(Duration timeout, Context context) {
Mono<Response<Boolean>> response = client.existsWithResponse(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.azure.core.annotation.ServiceClient;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.Response;
Expand All @@ -24,6 +25,7 @@
import com.azure.storage.file.share.implementation.models.SharesGetPropertiesResponse;
import com.azure.storage.file.share.implementation.models.SharesGetStatisticsResponse;
import com.azure.storage.file.share.implementation.util.ShareSasImplUtil;
import com.azure.storage.file.share.models.ShareErrorCode;
import com.azure.storage.file.share.models.ShareFileHttpHeaders;
import com.azure.storage.file.share.models.ShareRequestConditions;
import com.azure.storage.file.share.models.ShareSignedIdentifier;
Expand Down Expand Up @@ -152,6 +154,49 @@ public ShareFileAsyncClient getFileClient(String filePath) {
serviceVersion);
}

/**
* Determines if the share this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.file.share.ShareAsyncClient.exists}
*
* @return Flag indicating existence of the share.
*/
public Mono<Boolean> exists() {
return existsWithResponse().flatMap(FluxUtil::toMono);
}

/**
* Determines if the share this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.file.share.ShareAsyncClient.existsWithResponse}
*
* @return Flag indicating existence of the share.
*/
public Mono<Response<Boolean>> existsWithResponse() {
try {
return withContext(this::existsWithResponse);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}

Mono<Response<Boolean>> existsWithResponse(Context context) {
return this.getPropertiesWithResponse(context)
.map(cp -> (Response<Boolean>) new SimpleResponse<>(cp, true))
.onErrorResume(t ->
t instanceof ShareStorageException && ((ShareStorageException) t).getStatusCode() == 404
&& ((ShareStorageException) t).getErrorCode() == ShareErrorCode.SHARE_NOT_FOUND,
t -> {
HttpResponse response = ((ShareStorageException) t).getResponse();
return Mono.just(new SimpleResponse<>(response.getRequest(), response.getStatusCode(),
response.getHeaders(), false));
});
}

/**
* Creates the share in the storage account.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.List;
import java.util.Map;

import static com.azure.storage.common.implementation.StorageImplUtils.blockWithOptionalTimeout;

/**
* This class provides a client that contains all the operations for interacting with a share in Azure Storage Share.
* Operations allowed by the client are creating and deleting the share, creating snapshots for the share, creating and
Expand Down Expand Up @@ -105,6 +107,36 @@ public ShareFileClient getFileClient(String filePath) {
return new ShareFileClient(client.getFileClient(filePath));
}

/**
* Determines if the share this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.file.share.ShareClient.exists}
*
* @return Flag indicating existence of the share.
*/
public Boolean exists() {
return existsWithResponse(null, Context.NONE).getValue();
}

/**
* Determines if the share this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.file.share.ShareClient.existsWithResponse#Duration-Context}
*
* @param timeout An optional timeout value beyond which a {@link RuntimeException} will be raised.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @return Flag indicating existence of the share.
*/
public Response<Boolean> existsWithResponse(Duration timeout, Context context) {
Mono<Response<Boolean>> response = client.existsWithResponse(context);

return blockWithOptionalTimeout(response, timeout);
}

/**
* Creates the share in the storage account.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.azure.core.annotation.ServiceClient;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.Response;
Expand All @@ -28,6 +29,7 @@
import com.azure.storage.file.share.models.ShareDirectoryInfo;
import com.azure.storage.file.share.models.ShareDirectoryProperties;
import com.azure.storage.file.share.models.ShareDirectorySetMetadataInfo;
import com.azure.storage.file.share.models.ShareErrorCode;
import com.azure.storage.file.share.models.ShareFileHttpHeaders;
import com.azure.storage.file.share.models.ShareRequestConditions;
import com.azure.storage.file.share.models.ShareStorageException;
Expand Down Expand Up @@ -158,6 +160,54 @@ public ShareDirectoryAsyncClient getSubdirectoryClient(String subdirectoryName)
serviceVersion);
}

/**
* Determines if the directory this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.file.share.ShareDirectoryAsyncClient.exists}
*
* @return Flag indicating existence of the directory.
*/
public Mono<Boolean> exists() {
return existsWithResponse().flatMap(FluxUtil::toMono);
}

/**
* Determines if the directory this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.file.share.ShareDirectoryAsyncClient.existsWithResponse}
*
* @return Flag indicating existence of the directory.
*/
public Mono<Response<Boolean>> existsWithResponse() {
try {
return withContext(this::existsWithResponse);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}

Mono<Response<Boolean>> existsWithResponse(Context context) {
return this.getPropertiesWithResponse(context)
.map(cp -> (Response<Boolean>) new SimpleResponse<>(cp, true))
.onErrorResume(this::checkDoesNotExistStatusCode,
t -> {
HttpResponse response = ((ShareStorageException) t).getResponse();
return Mono.just(new SimpleResponse<>(response.getRequest(), response.getStatusCode(),
response.getHeaders(), false));
});
}

private boolean checkDoesNotExistStatusCode(Throwable t) {
return t instanceof ShareStorageException
&& ((ShareStorageException) t).getStatusCode() == 404
&& (((ShareStorageException) t).getErrorCode() == ShareErrorCode.RESOURCE_NOT_FOUND
|| ((ShareStorageException) t).getErrorCode() == ShareErrorCode.SHARE_NOT_FOUND);
}

/**
* Creates this directory in the file share and returns a response of {@link ShareDirectoryInfo} to interact
* with it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.time.Duration;
import java.util.Map;

import static com.azure.storage.common.implementation.StorageImplUtils.blockWithOptionalTimeout;

/**
* This class provides a client that contains all the operations for interacting with directory in Azure Storage File
* Service. Operations allowed by the client are creating, deleting and listing subdirectory and file, retrieving
Expand Down Expand Up @@ -100,6 +102,36 @@ public ShareDirectoryClient getSubdirectoryClient(String subdirectoryName) {
return new ShareDirectoryClient(shareDirectoryAsyncClient.getSubdirectoryClient(subdirectoryName));
}

/**
* Determines if the directory this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.file.share.ShareDirectoryClient.exists}
*
* @return Flag indicating existence of the directory.
*/
public Boolean exists() {
return existsWithResponse(null, Context.NONE).getValue();
}

/**
* Determines if the directory this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.file.share.ShareDirectoryClient.existsWithResponse#Duration-Context}
*
* @param timeout An optional timeout value beyond which a {@link RuntimeException} will be raised.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @return Flag indicating existence of the directory.
*/
public Response<Boolean> existsWithResponse(Duration timeout, Context context) {
Mono<Response<Boolean>> response = shareDirectoryAsyncClient.existsWithResponse(context);

return blockWithOptionalTimeout(response, timeout);
}

/**
* Creates a directory in the file share and returns a response of {@link ShareDirectoryInfo} to interact with it.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package com.azure.storage.file.share;

import com.azure.core.annotation.ServiceClient;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.Response;
Expand Down Expand Up @@ -42,6 +44,7 @@
import com.azure.storage.file.share.models.LeaseDurationType;
import com.azure.storage.file.share.models.LeaseStateType;
import com.azure.storage.file.share.models.LeaseStatusType;
import com.azure.storage.file.share.models.ShareErrorCode;
import com.azure.storage.file.share.models.ShareFileCopyInfo;
import com.azure.storage.file.share.models.ShareFileDownloadAsyncResponse;
import com.azure.storage.file.share.models.ShareFileHttpHeaders;
Expand Down Expand Up @@ -159,6 +162,66 @@ public ShareServiceVersion getServiceVersion() {
return serviceVersion;
}

/**
* Determines if the file this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.file.share.ShareFileAsyncClient.exists}
*
* @return Flag indicating existence of the file.
*/
public Mono<Boolean> exists() {
return existsWithResponse().flatMap(FluxUtil::toMono);
}

/**
* Determines if the file this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.file.share.ShareFileAsyncClient.existsWithResponse}
*
* @return Flag indicating existence of the file.
*/
public Mono<Response<Boolean>> existsWithResponse() {
try {
return withContext(this::existsWithResponse);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}

Mono<Response<Boolean>> existsWithResponse(Context context) {
return this.getPropertiesWithResponse(null, context)
.map(cp -> (Response<Boolean>) new SimpleResponse<>(cp, true))
.onErrorResume(this::checkDoesNotExistStatusCode,
t -> {
HttpResponse response = t instanceof ShareStorageException
? ((ShareStorageException) t).getResponse()
: ((HttpResponseException) t).getResponse();
return Mono.just(new SimpleResponse<>(response.getRequest(), response.getStatusCode(),
response.getHeaders(), false));
});
}

private boolean checkDoesNotExistStatusCode(Throwable t) {
// ShareStorageException
return (t instanceof ShareStorageException
&& ((ShareStorageException) t).getStatusCode() == 404
&& (((ShareStorageException) t).getErrorCode() == ShareErrorCode.RESOURCE_NOT_FOUND
|| ((ShareStorageException) t).getErrorCode() == ShareErrorCode.SHARE_NOT_FOUND))

/* HttpResponseException - file get properties is a head request so a body is not returned. Error
conversion logic does not properly handle errors that don't return XML. */
|| (t instanceof HttpResponseException
&& ((HttpResponseException) t).getResponse().getStatusCode() == 404
&& (((HttpResponseException) t).getResponse().getHeaderValue("x-ms-error-code")
.equals(ShareErrorCode.RESOURCE_NOT_FOUND.toString())
|| (((HttpResponseException) t).getResponse().getHeaderValue("x-ms-error-code")
.equals(ShareErrorCode.SHARE_NOT_FOUND.toString()))));
}

/**
* Creates a file in the storage account and returns a response of {@link ShareFileInfo} to interact with it.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import java.util.Map;
import java.util.Objects;

import static com.azure.storage.common.implementation.StorageImplUtils.blockWithOptionalTimeout;

/**
* This class provides a client that contains all the operations for interacting files under Azure Storage File Service.
* Operations allowed by the client are creating, uploading, copying, listing, downloading, and deleting files.
Expand Down Expand Up @@ -134,6 +136,36 @@ public final StorageFileOutputStream getFileOutputStream(long offset) {
return new StorageFileOutputStream(shareFileAsyncClient, offset);
}

/**
* Determines if the file this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.file.share.ShareFileClient.exists}
*
* @return Flag indicating existence of the file.
*/
public Boolean exists() {
return existsWithResponse(null, Context.NONE).getValue();
}

/**
* Determines if the file this client represents exists in the cloud.
*
* <p><strong>Code Samples</strong></p>
*
* {@codesnippet com.azure.storage.file.share.ShareFileClient.existsWithResponse#Duration-Context}
*
* @param timeout An optional timeout value beyond which a {@link RuntimeException} will be raised.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @return Flag indicating existence of the file.
*/
public Response<Boolean> existsWithResponse(Duration timeout, Context context) {
Mono<Response<Boolean>> response = shareFileAsyncClient.existsWithResponse(context);

return blockWithOptionalTimeout(response, timeout);
}

/**
* Creates a file in the storage account and returns a response of {@link ShareFileInfo} to interact with it.
*
Expand Down
Loading

0 comments on commit caec48f

Please sign in to comment.