/** * Creates the share if it does not exist. * * @return <code>true</code> if the share did not already exist and was created; otherwise, <code>false</code>. * * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public boolean createIfNotExists() throws StorageException { return this.createIfNotExists(null /* options */, null /* opContext */); }
/** * Returns a value that indicates whether the share exists. * * @return <code>true</code> if the share exists, otherwise <code>false</code>. * * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public boolean exists() throws StorageException { return this.exists(null /* accessCondition */, null /* options */, null /* opContext */); }
@Override public CloudFileShare preProcessResponse(CloudFileShare share, CloudFileClient client, OperationContext context) throws Exception { if (this.getResult().getStatusCode() != HttpURLConnection.HTTP_CREATED) { this.setNonExceptionedRetryableFailure(true); return null; } final String snapshotTime = FileResponse.getSnapshotTime(this.getConnection()); CloudFileShare snapshot = new CloudFileShare(share.getName(), snapshotTime, client); snapshot.setProperties(new FileShareProperties(share.properties)); // use the specified metadata if not null : otherwise share's metadata snapshot.setMetadata(metadata != null ? metadata : share.metadata); snapshot.updatePropertiesFromResponse(this.getConnection()); return snapshot; } };
/** * Returns the canonical name for shared access. * * @return the canonical name for shared access. */ private String getSharedAccessCanonicalName() { String accountName = this.getServiceClient().getCredentials().getAccountName(); String shareName = this.getName(); return String.format("/%s/%s/%s", SR.FILE, accountName, shareName); }
this.share.create(); assertTrue(this.share.exists()); HashMap<String, String> shareMeta = new HashMap<String, String>(); shareMeta.put("key1", "value1"); this.share.setMetadata(shareMeta); this.share.uploadMetadata(); CloudFileDirectory dir1 = this.share.getRootDirectoryReference().getDirectoryReference("dir1"); dir1.create(); CloudFile file1 = dir1.getFileReference("file1"); CloudFileShare snapshot = this.share.createSnapshot(); CloudFileClient client = FileTestHelper.createCloudFileClient(); CloudFileShare snapshotRef = client.getShareReference(snapshot.name, snapshot.snapshotID); assertTrue(snapshotRef.exists()); assertTrue(snapshotRef.getMetadata().size() == 1 && snapshotRef.getMetadata().get("key1").equals("value1")); snapshotRef2.downloadAttributes(); snapshot.downloadAttributes(); assertTrue(snapshotRef2.getMetadata().size() == 1 && snapshotRef2.getMetadata().get("key1").equals("value1")); assertTrue(snapshot.getMetadata().size() == 1 && snapshot.getMetadata().get("key1").equals("value1")); CloudFileDirectory snapshotDir1 = snapshot.getRootDirectoryReference().getDirectoryReference("dir1"); snapshotDir1.exists(); assertTrue(snapshotDir1.getMetadata().size() == 1 && snapshotDir1.getMetadata().get("key2").equals("value2")); CloudFileDirectory snapshotDir2 = snapshot.getRootDirectoryReference().getDirectoryReference("dir1"); snapshotDir2.downloadAttributes(); assertTrue(snapshotDir2.getMetadata().size() == 1 && snapshotDir2.getMetadata().get("key2").equals("value2"));
CloudFileShare share = fileClient.getShareReference(UUID.randomUUID().toString()); try { share.create(); share.setMetadata(shareMeta); share.uploadMetadata(); CloudFileShare snapshot = share.createSnapshot(); HashMap<String, String> meta2 = new HashMap<String, String>(); meta2.put("key2", "value2"); share.setMetadata(meta2); share.uploadMetadata(); boolean snapshotFound = false; for (CloudFileShare listShareItem : listResult) { if (listShareItem.getName().equals(share.getName()) && !listShareItem.isSnapshot() && !originalFound) { count++; originalFound = true; assertEquals(share.getMetadata(), listShareItem.getMetadata()); assertEquals(share.getStorageUri(), listShareItem.getStorageUri()); } else if (listShareItem.getName().equals(share.getName()) && listShareItem.isSnapshot() && !snapshotFound) { count++; snapshotFound = true; assertEquals(snapshot.getMetadata(), listShareItem.getMetadata()); assertEquals(snapshot.getStorageUri(), listShareItem.getStorageUri()); share.deleteIfExists(DeleteShareSnapshotsOption.INCLUDE_SNAPSHOTS, null, null, null);
this.share.uploadPermissions(permissions); Thread.sleep(30000); String shareReadListSas = this.share.generateSharedAccessSignature(policy2, null); CloudFileShare readListShare = new CloudFileShare(PathUtility.addToQuery(this.share.getUri(), shareReadListSas)); readListShare.getServiceClient().getCredentials().getClass().toString()); CloudFile fileFromSasShare = readListShare.getRootDirectoryReference().getFileReference(this.file.getName()); fileFromSasShare.download(new ByteArrayOutputStream()); CloudFileShare shareFromUri = new CloudFileShare(PathUtility.addToQuery( readListShare.getStorageUri(), this.share.generateSharedAccessSignature(null, "readlist"))); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), shareFromUri.getServiceClient().getCredentials().getClass().toString()); this.share.generateSharedAccessSignature(null, "readlist")); CloudFileClient client = new CloudFileClient(this.share.getServiceClient().getStorageUri(), creds); CloudFileShare shareFromClient = client.getShareReference(readListShare.getName()); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), shareFromClient.getServiceClient().getCredentials().getClass().toString()); assertEquals(client, shareFromClient.getServiceClient());
@Test public void testUnsupportedApisShareSnapshot() throws StorageException, URISyntaxException { CloudFileClient client = FileTestHelper.createCloudFileClient(); this.share.create(); this.share.downloadPermissions(); CloudFileShare snapshot = this.share.createSnapshot(); try { snapshot.createSnapshot(); fail("Shouldn't get here"); snapshot.downloadPermissions(); fail("Shouldn't get here"); snapshot.getStats(); fail("Shouldn't get here"); snapshot.uploadMetadata(); fail("Shouldn't get here"); snapshot.uploadPermissions(permissions); fail("Shouldn't get here"); snapshot.uploadProperties(); fail("Shouldn't get here"); snapshot.delete();
assertFalse(share.exists()); URI sasUri = sasClient.getShareReference(share.getName()).getUri(); sasUri = sasClient.getCredentials().transformUri(sasUri); final CloudFileShare sasShare = new CloudFileShare(sasUri); sasShare.create(); sasShare.create(); fail(); share.create(); assertTrue(share.exists()); assertEquals(sasShare.getName(), sasClient.listShares(sasShare.getName()).iterator().next().getName()); sasClient.listShares(sasShare.getName()).iterator().next(); fail(); assertEquals(sasShare.getName(), this.fileClient.listShares(sasShare.getName()).iterator().next().getName()); sasFile = sasShare.getRootDirectoryReference().getFileReference(FileTestHelper.generateRandomFileName()); if (policy.getPermissions().contains(SharedAccessAccountPermissions.CREATE) || policy.getPermissions().contains(SharedAccessAccountPermissions.WRITE)) { file = share.getRootDirectoryReference().getFileReference(sasFile.getName());
@Test public void testDeleteShareSnapshotOptions() throws StorageException, URISyntaxException, IOException { // create share with metadata this.share.create(); assertTrue(this.share.exists()); // verify that exists() call on snapshot populates metadata CloudFileShare snapshot = this.share.createSnapshot(); CloudFileClient client = FileTestHelper.createCloudFileClient(); CloudFileShare snapshotRef = client.getShareReference(snapshot.name, snapshot.snapshotID); assertTrue(snapshotRef.exists()); try { share.delete(); } catch (final StorageException e) { assertEquals(StorageErrorCodeStrings.SHARE_HAS_SNAPSHOTS, e.getErrorCode()); } share.delete(DeleteShareSnapshotsOption.INCLUDE_SNAPSHOTS, null, null, null); assertFalse(share.exists()); assertFalse(snapshot.exists()); }
public void testCloudFileShareQuota() throws StorageException, URISyntaxException { this.share.createIfNotExists(); this.share.downloadAttributes(); assertNotNull(this.share.getProperties().getShareQuota()); int shareQuota = FileConstants.MAX_SHARE_QUOTA; assertEquals(shareQuota, this.share.getProperties().getShareQuota().intValue()); this.share.getProperties().setShareQuota(shareQuota); this.share.uploadProperties(); this.share.downloadAttributes(); assertNotNull(this.share.getProperties().getShareQuota()); assertEquals(shareQuota, this.share.getProperties().getShareQuota().intValue()); this.share.delete(); this.share.getProperties().setShareQuota(shareQuota); this.share.create(); assertNotNull(this.share.getProperties().getShareQuota()); assertEquals(shareQuota, this.share.getProperties().getShareQuota().intValue()); this.share.downloadAttributes(); assertNotNull(this.share.getProperties().getShareQuota()); assertEquals(shareQuota, this.share.getProperties().getShareQuota().intValue()); this.share.getProperties().setShareQuota(shareQuota); fail(); } catch (IllegalArgumentException e) {
this.share.generateSharedAccessSignature(policy, null, null, SharedAccessProtocols.HTTPS_ONLY); String shareSasHttp = this.share.generateSharedAccessSignature(policy, null, null, SharedAccessProtocols.HTTPS_HTTP); final URI httpUri = new URI("http", this.share.getUri().getAuthority(), this.share.getUri().getPath(), this.share.getUri().getQuery(), null); final URI httpsUri = new URI("https", this.share.getUri().getAuthority(), this.share.getUri().getPath(), this.share.getUri().getQuery(), null); new CloudFileShare(PathUtility.addToQuery(httpUri, shareSasHttps)); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), httpShare.getServiceClient().getCredentials().getClass().toString()); CloudFile httpFile = httpShare.getRootDirectoryReference().getFileReference(this.file.getName()); httpFile.download(new ByteArrayOutputStream()); new CloudFileShare(PathUtility.addToQuery(httpsUri, shareSasHttps)); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), httpsShare.getServiceClient().getCredentials().getClass().toString()); CloudFile httpsFile = httpsShare.getRootDirectoryReference().getFileReference(this.file.getName()); httpsFile.download(new ByteArrayOutputStream()); new CloudFileShare(PathUtility.addToQuery(httpUri, shareSasHttp)); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), httpShare.getServiceClient().getCredentials().getClass().toString()); CloudFile httpFile = httpShare.getRootDirectoryReference().getFileReference(this.file.getName()); httpFile.download(new ByteArrayOutputStream()); new CloudFileShare(PathUtility.addToQuery(httpsUri, shareSasHttp)); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), httpsShare.getServiceClient().getCredentials().getClass().toString());
this.share.getMetadata().put("key1", "value1"); this.share.create(); assertEquals(1, this.share.getMetadata().size()); assertEquals("value1", this.share.getMetadata().get("key1")); CloudFileShare share2 = this.share.getServiceClient().getShareReference(this.share.getName()); share2.downloadAttributes(); assertEquals(1, share2.getMetadata().size()); assertEquals("value1", share2.getMetadata().get("key1")); this.share.getMetadata().put("key2", "value2"); assertEquals(2, this.share.getMetadata().size()); assertEquals("value1", this.share.getMetadata().get("key1")); assertEquals("value2", this.share.getMetadata().get("key2")); this.share.uploadMetadata(); assertEquals(2, this.share.getMetadata().size()); assertEquals("value1", this.share.getMetadata().get("key1")); assertEquals("value2", this.share.getMetadata().get("key2")); share2.downloadAttributes(); assertEquals(2, this.share.getMetadata().size()); assertEquals("value1", this.share.getMetadata().get("key1")); assertEquals("value2", this.share.getMetadata().get("key2")); Iterable<CloudFileShare> shares = this.share.getServiceClient().listShares(this.share.getName(), EnumSet.of(ShareListingDetails.METADATA), null, null);
/** * Test that Share Quota can be set, but only to allowable values. * * @throws StorageException * @throws URISyntaxException */ @Test public void testCloudFileShareQuotaListing() throws StorageException, URISyntaxException { int shareQuota = 16; this.share.getProperties().setShareQuota(shareQuota); this.share.createIfNotExists(); Iterable<CloudFileShare> shares = this.share.getServiceClient().listShares(this.share.getName()); for (CloudFileShare fileShare : shares) { assertEquals(shareQuota, fileShare.getProperties().getShareQuota().intValue()); } }
for (int i = 0; i < 30; i++) { shareList.add(prefix + i); fileClient.getShareReference(prefix + i).create(); share.downloadAttributes(); assertEquals(CloudFileShare.class, share.getClass()); shareList.remove(share.getName()); share.delete(); fileClient.getShareReference(shareName).deleteIfExists();
@Test public void testSupportedDirectoryApisInShareSnapshot() throws StorageException, URISyntaxException { CloudFileDirectory dir = this.share.getRootDirectoryReference().getDirectoryReference("dir1"); dir.deleteIfExists(); dir.create(); dir.setMetadata(meta); dir.uploadMetadata(); CloudFileShare snapshot = this.share.createSnapshot(); CloudFileDirectory snapshotDir = snapshot.getRootDirectoryReference().getDirectoryReference("dir1"); uriBuilder.add("sharesnapshot", snapshot.snapshotID); uriBuilder.add("restype", "directory"); CloudFileDirectory snapshotDir2 = new CloudFileDirectory(uriBuilder.addToURI(dir.getUri()), this.share.getServiceClient().getCredentials()); assertEquals(snapshot.snapshotID, snapshotDir2.getShare().snapshotID); assertTrue(snapshotDir2.exists()); snapshot.delete();
/** * Check a share's existence * * @throws StorageException */ @Test public void testCloudFileShareExists() throws StorageException { assertFalse(this.share.exists()); this.share.create(); assertTrue(this.share.exists()); assertNotNull(this.share.getProperties().getEtag()); this.share.delete(); assertFalse(this.share.exists()); }
@Test @Category({ SlowTests.class }) public void testShareSASCombinations() throws StorageException, URISyntaxException, IOException, InvalidKeyException, InterruptedException { EnumSet<SharedAccessFilePermissions> permissionSet = null; Map<Integer, CloudFileShare> shares = new HashMap<Integer, CloudFileShare>(); try { for (int bits = 0x1; bits < 0x40; bits++) { shares.put(bits, FileTestHelper.getRandomShareReference()); shares.get(bits).createIfNotExists(); permissionSet = getPermissions(bits); FileSharePermissions perms = new FileSharePermissions(); perms.getSharedAccessPolicies().put("readwrite" + bits, createSharedAccessPolicy(permissionSet, 300)); shares.get(bits).uploadPermissions(perms); } Thread.sleep(30000); for (int bits = 0x1; bits < 0x20; bits++) { permissionSet = getPermissions(bits); String sasToken = shares.get(bits).generateSharedAccessSignature(null, "readwrite" + bits); CloudFile testFile = FileTestHelper.uploadNewFile(shares.get(bits), 64, null); testAccess(sasToken, permissionSet, shares.get(bits), testFile); } } finally { for (int bits = 0x1; bits < shares.size(); bits++) { shares.get(bits).deleteIfExists(); } } }
throws StorageException, InterruptedException, URISyntaxException { CloudFileClient client = FileTestHelper.createCloudFileClient(); this.share.create(); FileSharePermissions permissions = this.share.downloadPermissions(); assertEquals(0, permissions.getSharedAccessPolicies().size()); this.share.uploadPermissions(permissions); Thread.sleep(30000); CloudFileShare share2 = client.getShareReference(this.share.getName()); assertPermissionsEqual(permissions, share2.downloadPermissions()); this.share.uploadPermissions(permissions); Thread.sleep(30000); assertPermissionsEqual(permissions, share2.downloadPermissions());
this.share.uploadPermissions(permissions); Thread.sleep(30000); String sasToken = this.share.generateSharedAccessSignature(policy, null); this.share.uploadPermissions(permissions); Thread.sleep(30000); String sasToken2 = this.share.generateSharedAccessSignature(policy2, null); CloudFileShare sasShare = new CloudFileShare(PathUtility.addToQuery(this.share.getUri(), sasToken2));