private void create() throws StorageException, URISyntaxException { this.fileShare.createIfNotExists(); //TODO: create method must provide file length, but we don't know actual //file size when creating. Pending on github issue feedback. this.cloudFile.create(1024); } }
/** * Creates a file. If the file already exists, this will replace it. * * @param size * A <code>long</code> which represents the size, in bytes, of the file. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException */ @DoesServiceRequest public void create(final long size) throws StorageException, URISyntaxException { this.create(size, null /* accessCondition */, null /* options */, null /* opContext */); }
/** * Creates a file. If the file already exists, this will replace it. * * @param size * A <code>long</code> which represents the size, in bytes, of the file. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException */ @DoesServiceRequest public void create(final long size) throws StorageException, URISyntaxException { this.create(size, null /* accessCondition */, null /* options */, null /* opContext */); }
@Test public void testCloudFileCreate() throws StorageException, URISyntaxException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file1"); assertFalse(file.exists()); // Create file.create(512); assertTrue(file.exists()); // Create again (should succeed) file.create(128); assertTrue(file.exists()); }
/** * Test file creation and deletion. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileCreateAndDelete() throws URISyntaxException, StorageException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file1"); file.create(0); assertTrue(file.exists()); file.delete(); }
/** * Ensures you cannot create a file without first creating the directory it is located within. * * @throws URISyntaxException * @throws StorageException */ public void testCloudFileDirectoryFileCreateWithoutDirectory() throws URISyntaxException, StorageException { CloudFileDirectory dir = this.share.getRootDirectoryReference().getDirectoryReference("Dir1"); CloudFile file = dir.getFileReference("file1"); try { file.create(0); fail("File shouldn't be created in a directory that wasn't created."); } catch (StorageException e) { assertEquals(e.getErrorCode(), "ParentNotFound"); assertEquals(e.getHttpStatusCode(), 404); assertEquals(e.getMessage(), "The specified parent path does not exist."); } // File creation directly in the share should pass. CloudFile file2 = this.share.getRootDirectoryReference().getFileReference("file2"); file2.create(0); dir.create(); file.create(0); }
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 file creation with metadata. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileCreateWithMetadata() throws URISyntaxException, StorageException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file1"); HashMap<String, String> meta = new HashMap<String, String>(); meta.put("key1", "value1"); file.setMetadata(meta); file.create(1024); CloudFile file2 = this.share.getRootDirectoryReference().getFileReference("file1"); file2.downloadAttributes(); assertEquals(1, file2.getMetadata().size()); assertEquals("value1", file2.getMetadata().get("key1")); file2.setMetadata(new HashMap<String, String>()); file2.uploadMetadata(); file.downloadAttributes(); assertEquals(0, file.getMetadata().size()); }
@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(); }
/** * Test file creation with invalid sizes. * * @throws URISyntaxException * @throws StorageException */ public void testCloudFileCreateInvalidSize() throws StorageException, URISyntaxException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file1"); try { file.create(-1); fail("Creating a file with size<0 should fail"); } catch (StorageException e) { assertEquals(e.getHttpStatusCode(), 400); assertEquals(e.getMessage(), "The value for one of the HTTP headers is not in the correct format."); } try { file.create(1L * 1024 * 1024 * 1024 * 1024 + 1); fail("Creating a file with size>1TB should fail"); } catch (StorageException e) { assertEquals(e.getHttpStatusCode(), 400); assertEquals(e.getMessage(), "The value for one of the HTTP headers is not in the correct format."); } }
fileRef.create(fileLengthToUse);
/** * Test file deleteIfExists. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileDeleteIfExists() throws URISyntaxException, StorageException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file1"); assertFalse(file.deleteIfExists()); file.create(0); assertTrue(file.deleteIfExists()); assertFalse(file.deleteIfExists()); }
/** * Test clearing file ranges. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileClearRange() throws URISyntaxException, StorageException, IOException { int fileLengthToUse = 8 * 512; byte[] buffer = FileTestHelper.getRandomBuffer(8 * 512); String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef.create(fileLengthToUse); // Upload one page (page 0) ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer); fileRef.uploadRange(inputStream, 0, fileLengthToUse); fileRef.clearRange(3 * 512, 2 * 512); byte[] result = new byte[fileLengthToUse]; fileRef.downloadToByteArray(result, 0); int i = 0; for (; i < 3 * 512; i++) { assertEquals(buffer[i], result[i]); } for (; i < 5 * 512; i++) { assertEquals(0, result[i]); } for (; i < 8 * 512; i++) { assertEquals(buffer[i], result[i]); } }
/** * Test requesting stored content MD5 with OpenWriteExisting(). * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileOpenWriteExistingWithMD5() throws URISyntaxException, StorageException, IOException { String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef.create(512); FileRequestOptions options = new FileRequestOptions(); options.setStoreFileContentMD5(true); options.setDisableContentMD5Validation(false); try { fileRef.openWriteExisting(null, options, null); fail("Expect failure due to requesting MD5 calculation"); } catch (IllegalArgumentException e) { } }
dir.create(); CloudFile file = dir.getFileReference("file"); file.create(1024);
/** * Test file exits method. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileExists() throws URISyntaxException, StorageException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file1"); CloudFile file2 = this.share.getRootDirectoryReference().getFileReference("file1"); assertFalse(file2.exists()); file.create(2048); assertTrue(file2.exists()); assertEquals(2048, file2.getProperties().getLength()); file.delete(); assertFalse(file2.exists()); }
public void testCloudFileDownloadAttributes() throws URISyntaxException, StorageException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file29"); file.create(1024); assertEquals(1024, file.getProperties().getLength()); assertNotNull(file.getProperties().getEtag());
/** * Test file resizing. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileResize2() throws StorageException, URISyntaxException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file1"); CloudFile file2 = this.share.getRootDirectoryReference().getFileReference("file1"); file.create(1024); assertEquals(1024, file.getProperties().getLength()); file2.downloadAttributes(); assertEquals(1024, file2.getProperties().getLength()); file2.getProperties().setContentType("text/plain"); file2.uploadProperties(); file.resize(2048); assertEquals(2048, file.getProperties().getLength()); file.downloadAttributes(); assertEquals("text/plain", file.getProperties().getContentType()); file2.downloadAttributes(); assertEquals(2048, file2.getProperties().getLength()); }
myDir.getFileReference("myfile").create(1024); myDir.getDirectoryReference("yourDir").create(); assertTrue(this.share.exists());
/** * Test file resizing. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileResize() throws URISyntaxException, StorageException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file1"); CloudFile file2 = this.share.getRootDirectoryReference().getFileReference("file1"); file.create(1024); assertEquals(1024, file.getProperties().getLength()); file2.downloadAttributes(); assertEquals(1024, file2.getProperties().getLength()); file2.getProperties().setContentType("text/plain"); file2.uploadProperties(); file.resize(2048); assertEquals(2048, file.getProperties().getLength()); file.downloadAttributes(); assertEquals("text/plain", file.getProperties().getContentType()); file2.downloadAttributes(); assertEquals(2048, file2.getProperties().getLength()); // Resize to 0 length file.resize(0); assertEquals(0, file.getProperties().getLength()); file.downloadAttributes(); assertEquals("text/plain", file.getProperties().getContentType()); file2.downloadAttributes(); assertEquals(0, file2.getProperties().getLength()); }