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); } }
public static CloudFile uploadNewFile( CloudFileShare share, InputStream stream, int length, OperationContext context) throws StorageException, IOException, URISyntaxException { String name = generateRandomFileName(); CloudFile file = null; file = share.getRootDirectoryReference().getFileReference(name); file.upload(stream, length, null, null, context); return file; }
@Test public void testFileContentMD5NewFileTest() throws URISyntaxException, StorageException, IOException { final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile file = this.share.getRootDirectoryReference().getFileReference(fileName); FileRequestOptions options = new FileRequestOptions(); options.setStoreFileContentMD5(true); options.setDisableContentMD5Validation(false); File tempFile = File.createTempFile("sourceFile", ".tmp"); file.uploadFromFile(tempFile.getAbsolutePath(), null, options, null); }
@Test public void testCloudFileDownloadRangeToByteArrayNegativeTest() throws URISyntaxException, StorageException, IOException { CloudFile file = this.share.getRootDirectoryReference().getFileReference( FileTestHelper.generateRandomFileName()); FileTestHelper.doDownloadRangeToByteArrayNegativeTests(file); }
@Test public void testFileUploadFromStreamTest() throws URISyntaxException, StorageException, IOException { final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); int length = 2 * 1024; ByteArrayInputStream srcStream = FileTestHelper.getRandomDataStream(length); fileRef.upload(srcStream, length); ByteArrayOutputStream dstStream = new ByteArrayOutputStream(); fileRef.download(dstStream); FileTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); length = 5 * 1024 * 1024; srcStream = FileTestHelper.getRandomDataStream(length); fileRef.upload(srcStream, length); dstStream = new ByteArrayOutputStream(); fileRef.download(dstStream); FileTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); }
/** * 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(); }
/** * Test file upload and download using text. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileUploadDownloadFromText() throws IOException, StorageException, URISyntaxException { String fileName = FileTestHelper.generateRandomFileName(); final CloudFile file = this.share.getRootDirectoryReference().getFileReference(fileName); String textFile = "string of text to upload to a file. The_quick_brown_fox_jumps_over_the_lazy_dog. 1!\". "."; file.uploadText(textFile); String result = file.downloadText(); assertEquals(textFile, result); }
@Test public void testCloudFileDownloadToByteArray() throws URISyntaxException, StorageException, IOException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file1"); FileTestHelper.doDownloadTest(file, 1 * 512, 2 * 512, 0); FileTestHelper.doDownloadTest(file, 1 * 512, 2 * 512, 1 * 512); FileTestHelper.doDownloadTest(file, 2 * 512, 4 * 512, 1 * 512); FileTestHelper.doDownloadTest(file, 5 * 1024 * 1024, 5 * 1024 * 1024, 0); FileTestHelper.doDownloadTest(file, 5 * 1024 * 1024, 6 * 1024 * 1024, 512); }
/** * 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"); }
@Test public void testFileEmptyHeaderSigningTest() throws URISyntaxException, StorageException, IOException { final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); final int length = 2 * 1024; ByteArrayInputStream srcStream = FileTestHelper.getRandomDataStream(length); OperationContext context = new OperationContext(); context.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() { @Override public void eventOccurred(SendingRequestEvent eventArg) { HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject(); connection.setRequestProperty("x-ms-foo", ""); } }); fileRef.upload(srcStream, length, null, null, context); fileRef.download(new ByteArrayOutputStream(), null, null, context); }
@Before public void fileEncryptionTestMethodSetup() throws URISyntaxException, StorageException, IOException { this.share = FileTestHelper.getRandomShareReference(); this.share.create(); this.dir = this.share.getRootDirectoryReference().getDirectoryReference("dir"); this.dir.create(); this.file = this.share.getRootDirectoryReference().getFileReference("file"); this.file.uploadText("text"); }
/** * Test file uploading from byte arrays. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileUploadFromByteArray() throws Exception { String fileName = FileTestHelper.generateRandomFileName(); final CloudFile file = this.share.getRootDirectoryReference().getFileReference(fileName); this.doUploadFromByteArrayTest(file, 4 * 512, 0, 4 * 512); this.doUploadFromByteArrayTest(file, 4 * 512, 0, 2 * 512); this.doUploadFromByteArrayTest(file, 4 * 512, 1 * 512, 2 * 512); this.doUploadFromByteArrayTest(file, 4 * 512, 2 * 512, 2 * 512); }
/** * Test file upload and download using io files. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileUploadDownloadFromFile() throws IOException, StorageException, URISyntaxException { String fileName = FileTestHelper.generateRandomFileName(); final CloudFile file = this.share.getRootDirectoryReference().getFileReference(fileName); this.doUploadDownloadFileTest(file, 512); this.doUploadDownloadFileTest(file, 4096); this.doUploadDownloadFileTest(file, 5 * 1024 * 1024); this.doUploadDownloadFileTest(file, 11 * 1024 * 1024); }
@Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testVerifyTransactionalMD5ValidationMissingOverallMD5() throws URISyntaxException, StorageException, IOException { final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); final int length = 3*1024; ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length); FileRequestOptions options = new FileRequestOptions(); options.setDisableContentMD5Validation(true); options.setStoreFileContentMD5(false); fileRef.upload(srcStream, length, null, options, null); options.setDisableContentMD5Validation(false); options.setStoreFileContentMD5(true); options.setUseTransactionalContentMD5(true); final CloudFile fileRef2 = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef2.downloadRange(1024, (long)1024, new ByteArrayOutputStream(), null, options, null); assertNull(fileRef2.getProperties().getContentMD5()); }
/** * 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()); }
/** * 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 FileOutputStream. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileOpenOutputStreamNoArgs() throws URISyntaxException, StorageException { String fileName = FileTestHelper.generateRandomFileName(); CloudFile file = this.share.getRootDirectoryReference().getFileReference(fileName); try { file.openWriteExisting(); } catch (StorageException ex) { assertEquals("The specified resource does not exist.", ex.getMessage()); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode()); } file.openWriteNew(1024); file.openWriteExisting(); CloudFile file2 = this.share.getRootDirectoryReference().getFileReference(fileName); file2.downloadAttributes(); assertEquals(1024, file2.getProperties().getLength()); }
@Test public void testFileAttributesEncryption() throws URISyntaxException, StorageException, IOException { this.file.downloadAttributes(); assertTrue(this.file.getProperties().isServerEncrypted()); CloudFile testFile = this.share.getRootDirectoryReference().getFileReference("file"); testFile.downloadText(); assertTrue(testFile.getProperties().isServerEncrypted()); }
@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()); }
/** * Create a file and try to download a range of its contents * * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test public void testFileDownloadRangeValidationTest() throws StorageException, URISyntaxException, IOException { final int length = 5 * 1024 * 1024; final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef.upload(FileTestHelper.getRandomDataStream(length), length); //Download full file fileRef.download(new ByteArrayOutputStream()); assertEquals(length, fileRef.getProperties().getLength()); //Download file range. byte[] downloadBuffer = new byte[100]; int downloadLength = fileRef.downloadRangeToByteArray(0, (long) 100, downloadBuffer, 0); assertEquals(length, fileRef.getProperties().getLength()); assertEquals(100, downloadLength); }