FileStorageResource(CloudFileClient fileClient, String location, boolean autoCreateFiles) { assertIsAzureStorageLocation(location); this.autoCreateFiles = autoCreateFiles; this.fileClient = fileClient; this.location = location; try { this.fileShare = fileClient.getShareReference(getContainerName(location)); this.cloudFile = fileShare.getRootDirectoryReference().getFileReference(getFileName(location)); } catch (URISyntaxException | StorageException e) { log.error(MSG_FAIL_GET, e); throw new RuntimeException(MSG_FAIL_GET, e); } }
/** * Tests directory hierarchy navigation. * * @throws URISyntaxException * @throws StorageException */ public void testCloudFileDirectoryFileParentValidate() throws URISyntaxException, StorageException { CloudFile file = this.share.getRootDirectoryReference().getDirectoryReference("TopDir1") .getDirectoryReference("MidDir1").getDirectoryReference("EndDir1").getFileReference("EndFile1"); CloudFileDirectory directory = file.getParent(); assertEquals(directory.getName(), "EndDir1"); assertEquals(directory.getUri().toString(), this.share.getUri() + "/TopDir1/MidDir1/EndDir1"); }
/** * Returns the {@link CloudFileDirectory} parent directory associated with this directory. * * @return An {@link CloudFileDirectory} object that represents the parent directory associated with the directory. * @throws StorageException * @throws URISyntaxException */ @Override public CloudFileDirectory getParent() throws URISyntaxException, StorageException { if (this.parent == null) { final String parentName = CloudFile.getParentNameFromURI(this.getStorageUri(), this.getShare()); if (parentName != null) { StorageUri parentURI = PathUtility.appendPathToUri(this.getShare().getStorageUri(), parentName); this.parent = new CloudFileDirectory(parentURI, this.getServiceClient().getCredentials()); } } return this.parent; }
/** * Returns a reference to a {@link CloudFileDirectory} object that represents a directory in this directory. * * @param itemName * A <code>String</code> that represents the name of the directory. * * @return A {@link CloudFileDirectory} object that represents a reference to the specified directory. * * @throws URISyntaxException * If the resource URI is invalid. * @throws StorageException */ public CloudFileDirectory getDirectoryReference(final String itemName) throws URISyntaxException, StorageException { Utility.assertNotNullOrEmpty("itemName", itemName); StorageUri subdirectoryUri = PathUtility.appendPathToUri(this.storageUri, itemName); return new CloudFileDirectory(subdirectoryUri, itemName, this.getShare()); }
private boolean doCloudFileDirectorySetup(CloudFileShare share) throws URISyntaxException, StorageException { try { CloudFileDirectory rootDirectory = share.getRootDirectoryReference(); for (int i = 1; i < 3; i++) { CloudFileDirectory topDirectory = rootDirectory.getDirectoryReference("TopDir" + i); topDirectory.create(); for (int j = 1; j < 3; j++) { CloudFileDirectory midDirectory = topDirectory.getDirectoryReference("MidDir" + j); midDirectory.create(); for (int k = 1; k < 3; k++) { CloudFileDirectory endDirectory = midDirectory.getDirectoryReference("EndDir" + k); endDirectory.create(); CloudFile file1 = endDirectory.getFileReference("EndFile" + k); file1.create(0); } } CloudFile file2 = topDirectory.getFileReference("File" + i); file2.create(0); } return true; } catch (StorageException e) { throw e; } }
@Test public void testSupportedDirectoryApisInShareSnapshot() throws StorageException, URISyntaxException { CloudFileDirectory dir = this.share.getRootDirectoryReference().getDirectoryReference("dir1"); dir.deleteIfExists(); dir.create(); HashMap<String, String> meta = new HashMap<String, String>(); meta.put("key1", "value1"); dir.setMetadata(meta); dir.uploadMetadata(); CloudFileShare snapshot = this.share.createSnapshot(); CloudFileDirectory snapshotDir = snapshot.getRootDirectoryReference().getDirectoryReference("dir1"); dir.setMetadata(meta2); dir.uploadMetadata(); snapshotDir.downloadAttributes(); assertTrue(snapshotDir.getMetadata().size() == 1 && snapshotDir.getMetadata().get("key1").equals("value1")); assertNotNull(snapshotDir.getProperties().getEtag()); dir.downloadAttributes(); assertTrue(dir.getMetadata().size() == 1 && dir.getMetadata().get("key2").equals("value2")); assertNotNull(dir.getProperties().getEtag()); assertNotEquals(dir.getProperties().getEtag(), snapshotDir.getProperties().getEtag()); CloudFileDirectory snapshotDir2 = new CloudFileDirectory(uriBuilder.addToURI(dir.getUri()), this.share.getServiceClient().getCredentials()); assertEquals(snapshot.snapshotID, snapshotDir2.getShare().snapshotID); assertTrue(snapshotDir2.exists());
@Test public void testDirectorySas() throws InvalidKeyException, IllegalArgumentException, StorageException, URISyntaxException, InterruptedException { CloudFileDirectory dir = this.share.getRootDirectoryReference().getDirectoryReference("dirFile"); CloudFile file = dir.getFileReference("dirFile"); dir.create(); file.create(512); SharedAccessFilePolicy policy = createSharedAccessPolicy( EnumSet.of(SharedAccessFilePermissions.READ, SharedAccessFilePermissions.LIST, SharedAccessFilePermissions.DELETE), 300); // Test directory SAS with a file SAS token from an identically named file String sas = file.generateSharedAccessSignature(policy, null); CloudFileDirectory sasDir = new CloudFileDirectory(new URI(dir.getUri().toString() + "?" + sas)); try { sasDir.downloadAttributes(); fail("This should result in an authentication error."); } catch (StorageException ex) { assertEquals(HttpURLConnection.HTTP_FORBIDDEN, ex.getHttpStatusCode()); } // Test directory SAS with a share SAS token sas = this.share.generateSharedAccessSignature(policy, null); sasDir = new CloudFileDirectory(new URI(dir.getUri().toString() + "?" + sas)); sasDir.downloadAttributes(); // Test deleting a directory using a SAS token. The directory must be empty for this request to succeed. file.delete(); sasDir.delete(); }
this.share.uploadMetadata(); CloudFileDirectory dir1 = this.share.getRootDirectoryReference().getDirectoryReference("dir1"); dir1.create(); CloudFile file1 = dir1.getFileReference("file1"); file1.create(1024); ByteArrayInputStream srcStream = FileTestHelper.getRandomDataStream(1024); dir1.setMetadata(dirMeta); dir1.uploadMetadata(); 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"));
/** * Test creation and deletion of a directory. * * @throws URISyntaxException * @throws StorageException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class, CloudTests.class }) public void testCloudFileDirectoryCreateAndDelete() throws URISyntaxException, StorageException { CloudFileDirectory directory = this.share.getRootDirectoryReference().getDirectoryReference("directory1"); directory.create(); assertTrue(directory.exists()); directory.delete(); assertFalse(directory.exists()); }
/** * Tests the CloudFileDirectory constructor. * * @throws URISyntaxException * @throws StorageException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class, CloudTests.class }) public void testCloudFileDirectoryConstructor() throws URISyntaxException, StorageException { CloudFileDirectory directory = this.share.getRootDirectoryReference().getDirectoryReference("directory1"); CloudFileDirectory directory2 = new CloudFileDirectory(directory.getStorageUri(), this.share.getServiceClient().getCredentials()); assertEquals(directory.getName(), directory2.getName()); assertEquals(directory.getStorageUri(), directory2.getStorageUri()); assertEquals(directory.getShare().getStorageUri(), directory2.getShare().getStorageUri()); assertEquals(FileTestHelper.ensureTrailingSlash(directory.getServiceClient().getStorageUri()), FileTestHelper.ensureTrailingSlash(directory2.getServiceClient().getStorageUri())); }
public void testCloudFileDirectoryListFilesAndDirectories() throws StorageException, URISyntaxException { if (doCloudFileDirectorySetup(this.share)) { CloudFileDirectory topDir1 = this.share.getRootDirectoryReference().getDirectoryReference("TopDir1"); Iterable<ListFileItem> list1 = topDir1.listFilesAndDirectories(); assertTrue(list1.iterator().hasNext()); ArrayList<ListFileItem> simpleList1 = new ArrayList<ListFileItem>(); assertEquals("MidDir1", ((CloudFileDirectory) item12).getName()); assertEquals("MidDir2", ((CloudFileDirectory) item13).getName()); CloudFileDirectory midDir2 = (CloudFileDirectory) item13; Iterable<ListFileItem> list2 = midDir2.listFilesAndDirectories(); assertEquals("EndDir1", ((CloudFileDirectory) item21).getName()); assertEquals("EndDir2", ((CloudFileDirectory) item22).getName()); topDir1.downloadAttributes(); assertNotNull(topDir1.getProperties().getEtag()); assertNotNull(topDir1.getProperties().getLastModified()); ResultSegment<ListFileItem> segment1 = topDir1.listFilesAndDirectoriesSegmented(); assertFalse(segment1.getHasMoreResults());
this.share.create(); CloudFileDirectory myDir = this.share.getRootDirectoryReference().getDirectoryReference("mydir"); myDir.create(); myDir.getFileReference("myfile").create(1024); myDir.getDirectoryReference("yourDir").create(); assertTrue(this.share.exists()); CloudFileShare snapshotRef = client.getShareReference(snapshot.name, snapshot.snapshotID); Iterable<ListFileItem> listResult = snapshotRef.getRootDirectoryReference().listFilesAndDirectories(); int count = 0; for (ListFileItem listFileItem : listResult) { count++; assertEquals("mydir", ((CloudFileDirectory) listFileItem).getName()); listResult = snapshotRef.getRootDirectoryReference().getDirectoryReference("mydir").listFilesAndDirectories(); for (ListFileItem listFileItem : listResult) { if (listFileItem instanceof CloudFileDirectory) { count++; assertEquals("yourDir", ((CloudFileDirectory) listFileItem).getName());
/** * Tests to make sure you can't delete a directory if it is nonempty. * * @throws URISyntaxException * @throws StorageException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class, CloudTests.class }) public void testCloudFileDirectoryWithFilesDelete() throws URISyntaxException, StorageException { if (doCloudFileDirectorySetup(this.share)) { CloudFileDirectory dir1 = this.share.getRootDirectoryReference().getDirectoryReference( "TopDir1/MidDir1/EndDir1"); CloudFile file1 = dir1.getFileReference("EndFile1"); try { dir1.delete(); fail("Shouldn't be able to delete directory when files still exist in it."); } catch (StorageException e) { assertEquals(e.getHttpStatusCode(), 409); assertEquals(e.getMessage(), "The specified directory is not empty."); } file1.delete(); dir1.delete(); assertFalse(file1.exists()); assertFalse(dir1.exists()); } }
/** * Check if a directory reference with metadata will still have that metadata after being created. * * @throws StorageException * @throws URISyntaxException */ @Test public void testCreateDirectoryWithMetadata() throws StorageException, URISyntaxException { String directoryName = "newDirectory1"; CloudFileDirectory directory = new CloudFileDirectory( PathUtility.appendPathToUri(this.share.getStorageUri(), directoryName), directoryName, this.share); Assert.assertEquals(0, directory.getMetadata().size()); directory.getMetadata().put("key1", "value1"); directory.createIfNotExists(); directory.getMetadata().clear(); directory.downloadAttributes(); Assert.assertEquals(1, directory.getMetadata().size()); Assert.assertEquals("value1", directory.getMetadata().get("key1")); }
@Test public void testSupportedFileApisInShareSnapshot() throws StorageException, URISyntaxException, UnsupportedEncodingException { CloudFileDirectory dir = this.share.getRootDirectoryReference().getDirectoryReference("dir1"); dir.deleteIfExists(); dir.create(); CloudFile file = dir.getFileReference("file"); file.create(1024); .getDirectoryReference("dir1").getFileReference("file");
CloudFile retFile = null; try { retFile = this.directory.getFileReference(this.name); CloudFileDirectory retDirectory = null; try { retDirectory = this.directory.getDirectoryReference(this.name); throw new SAXException(e); retDirectory.setProperties(this.directoryProperties); this.response.getResults().add(retDirectory);
/** * Tests deleteIfExists for a directory. * * @throws URISyntaxException * @throws StorageException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class, CloudTests.class }) public void testCloudFileDirectoryDeleteIfExists() throws URISyntaxException, StorageException { CloudFileDirectory directory = this.share.getRootDirectoryReference().getDirectoryReference("directory1"); assertFalse(directory.deleteIfExists()); directory.create(); assertTrue(directory.deleteIfExists()); assertFalse(directory.deleteIfExists()); }
/** * Tests createIfNotExists for a directory. * * @throws StorageException * @throws URISyntaxException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class, CloudTests.class }) public void testCloudFileDirectoryCreateIfNotExists() throws StorageException, URISyntaxException { CloudFileDirectory directory = this.share.getRootDirectoryReference().getDirectoryReference("directory1"); assertTrue(directory.createIfNotExists()); assertFalse(directory.createIfNotExists()); directory.delete(); assertTrue(directory.createIfNotExists()); }
@Test public void testDirectoryAttributesEncryption() throws URISyntaxException, StorageException, IOException { assertFalse(this.dir.getProperties().isServerEncrypted()); CloudFileDirectory testDir = this.share.getRootDirectoryReference().getDirectoryReference("dir"); testDir.downloadAttributes(); assertTrue(testDir.getProperties().isServerEncrypted()); }