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); } }
/** * Downloads the contents of a file to a stream. * * @param outStream * An <code>{@link OutputStream}</code> object that represents the target stream. * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public final void download(final OutputStream outStream) throws StorageException { this.download(outStream, null /* accessCondition */, null /* options */, null /* opContext */); }
/** * Checks to see if the file exists. * * @return <code>true</code> if the file exists, otherwise <code>false</code>. * * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public final boolean exists() throws StorageException { return this.exists(null /* accessCondition */, null /* options */, null /* opContext */); }
OutputStream outputStream = new BufferedOutputStream(new java.io.FileOutputStream(path)); try { this.download(outputStream, accessCondition, options, opContext); outputStream.close(); deleteEmptyFileOnException(outputStream, path); throw e; deleteEmptyFileOnException(outputStream, path); throw e;
/** * Returns the file item's parent. * * @return A {@link CloudFileDirectory} object that represents the parent directory for the file. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException * If the resource URI is invalid. */ @Override public final CloudFileDirectory getParent() throws URISyntaxException, StorageException { if (this.parent == null) { final String parentName = getParentNameFromURI(this.getStorageUri(), this.getShare()); if (parentName != null) { StorageUri parentURI = PathUtility.appendPathToUri(this.share.getStorageUri(), parentName); this.parent = new CloudFileDirectory(parentURI, this.getServiceClient().getCredentials()); } } return this.parent; }
public static void assertAreEqual(CloudFile file1, CloudFile file2) { if (file1 == null) { Assert.assertNull(file2); } else { Assert.assertNotNull(file2); Assert.assertEquals(file1.getUri(), file2.getUri()); assertAreEqual(file1.getProperties(), file2.getProperties()); } }
source.getMetadata().put("Test", "value"); source.uploadText(data, Constants.UTF8_CHARSET, null, null, null); String sasToken = source.generateSharedAccessSignature(policy, null, null); copySource = new CloudFile(credentials.transformUri(source.getUri())); String sasToken = destination.generateSharedAccessSignature(policy, null, null); copyDestination = new CloudFile(destination.getUri(), credentials); String copyId = copyDestination.startCopy(copySource); FileTestHelper.waitForCopy(copyDestination); destination.downloadAttributes(); assertEquals(CopyStatus.SUCCESS, destination.getCopyState().getStatus()); assertEquals(source.getServiceClient().getCredentials().transformUri(source.getUri()).getPath(), destination.getCopyState().getSource().getPath()); assertEquals(data.length(), destination.getCopyState().getTotalBytes().intValue()); assertEquals(data.length(), destination.getCopyState().getBytesCopied().intValue()); assertEquals(copyId, destination.getProperties().getCopyState().getCopyId()); copyDestination.abortCopy(destination.getCopyState().getCopyId()); assertNotNull(destination.getProperties().getEtag()); assertFalse(source.getProperties().getEtag().equals(destination.getProperties().getEtag())); String copyData = destination.downloadText(Constants.UTF8_CHARSET, null, null, null); assertEquals(data, copyData);
fileRef.upload(srcStream, length, null, options, null); fileRef.downloadAttributes(); fileRef.getProperties().setContentMD5("MDAwMDAwMDA="); fileRef.uploadProperties(null, options, null); fileRef.download(new ByteArrayOutputStream(), null, options, null); fail(); fileRef.download(new ByteArrayOutputStream(), null, options, null); fileRef.downloadAttributes(); fileRef.getProperties().setContentMD5(calculatedMD5); fileRef.uploadProperties(null, options, null); fileRef.download(new ByteArrayOutputStream(), null, options, null); fileRef.downloadToByteArray(new byte[4096], 0); FileInputStream stream = fileRef.openRead(); stream.mark(length); stream.read(new byte[4096]); HashMap<String, String> metadata = new HashMap<String, String>(); metadata.put("a", "value"); fileRef.setMetadata(metadata); fileRef.uploadMetadata(); stream.reset(); stream.read(new byte[4096]); assertNull(fileRef2.getProperties().getContentMD5());
dir.create(); CloudFile file = dir.getFileReference("file"); file.create(1024); file.setMetadata(meta); file.uploadMetadata(); file.setMetadata(meta2); file.uploadMetadata(); snapshotFile.downloadAttributes(); assertTrue(snapshotFile.getMetadata().size() == 1 && snapshotFile.getMetadata().get("key1").equals("value1")); assertNotNull(snapshotFile.getProperties().getEtag()); file.downloadAttributes(); assertTrue(file.getMetadata().size() == 1 && file.getMetadata().get("key2").equals("value2")); assertNotNull(file.getProperties().getEtag()); assertNotEquals(file.getProperties().getEtag(), snapshotFile.getProperties().getEtag()); CloudFile snapshotFile2 = new CloudFile(uriBuilder.addToURI(file.getUri()), this.share.getServiceClient().getCredentials()); assertEquals(snapshot.snapshotID, snapshotFile2.getShare().snapshotID); assertTrue(snapshotFile2.exists());
options.setDisableContentMD5Validation(true); source.getMetadata().put("Test", "value"); source.uploadText(data); policy.setSharedAccessExpiryTime(calendar.getTime()); String sasToken = source.generateSharedAccessSignature(policy, null, null); FileTestHelper.defiddler(new CloudFile(credentials.transformUri(source.getUri())))); BlobTestHelper.waitForCopy(destination); destination.downloadAttributes(); assertEquals(source.getServiceClient().getCredentials().transformUri(source.getUri()).getPath(), destination.getCopyState().getSource().getPath()); assertEquals(data.length(), destination.getCopyState().getTotalBytes().intValue()); assertEquals(data, copyData); source.downloadAttributes(); BlobProperties prop1 = destination.getProperties(); FileProperties prop2 = source.getProperties();
source.getMetadata().put("Test", "value"); source.uploadText(data); copy.getMetadata().put("Test2", "value2"); String copyId = copy.startCopy(FileTestHelper.defiddler(source)); FileTestHelper.waitForCopy(copy); assertEquals(CopyStatus.SUCCESS, copy.getCopyState().getStatus()); assertEquals(source.getServiceClient().getCredentials().transformUri(source.getUri()).getPath(), copy.getCopyState().getSource().getPath()); assertEquals(data.length(), copy.getCopyState().getTotalBytes().intValue()); assertEquals(data.length(), copy.getCopyState().getBytesCopied().intValue()); assertEquals(copyId, copy.getCopyState().getCopyId()); assertTrue(copy.getCopyState().getCompletionTime().compareTo(new Date(calendar.get(Calendar.MINUTE) - 1)) > 0); String copyData = copy.downloadText(Constants.UTF8_CHARSET, null, options, null); assertEquals(data, copyData); copy.downloadAttributes(); source.downloadAttributes(); FileProperties prop1 = copy.getProperties(); FileProperties prop2 = source.getProperties(); assertEquals("value2", copy.getMetadata().get("Test2")); assertFalse(copy.getMetadata().containsKey("Test")); copy.delete();
@Test public void testFileSAS() throws InvalidKeyException, IllegalArgumentException, StorageException, URISyntaxException, InterruptedException { SharedAccessFilePolicy policy = createSharedAccessPolicy( EnumSet.of(SharedAccessFilePermissions.READ, SharedAccessFilePermissions.LIST), 300); FileSharePermissions perms = new FileSharePermissions(); perms.getSharedAccessPolicies().put("readperm", policy); this.share.uploadPermissions(perms); Thread.sleep(30000); CloudFile sasFile = new CloudFile( new URI(this.file.getUri().toString() + "?" + this.file.generateSharedAccessSignature(null, "readperm"))); sasFile.download(new ByteArrayOutputStream()); // do not give the client and check that the new file's client has the correct permissions CloudFile fileFromUri = new CloudFile(PathUtility.addToQuery(this.file.getStorageUri(), this.file.generateSharedAccessSignature(null, "readperm"))); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), fileFromUri.getServiceClient().getCredentials().getClass().toString()); // create credentials from sas StorageCredentials creds = new StorageCredentialsSharedAccessSignature( this.file.generateSharedAccessSignature(policy, null, null)); CloudFileClient client = new CloudFileClient(sasFile.getServiceClient().getStorageUri(), creds); CloudFile fileFromClient = client.getShareReference(this.file.getShare().getName()).getRootDirectoryReference() .getFileReference(this.file.getName()); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), fileFromClient.getServiceClient().getCredentials().getClass().toString()); assertEquals(client, fileFromClient.getServiceClient()); }
if (share != null) { share = new CloudFileShare(credentials.transformUri(share.getUri())); file = share.getRootDirectoryReference().getFileReference(file.getName()); file = new CloudFile(credentials.transformUri(file.getUri())); file.downloadAttributes(); file.downloadAttributes(); fail(); file.downloadAttributes(); file.downloadAttributes(); Assert.fail(); final StorageUri uri = PathUtility.appendPathToUri( share.getStorageUri(), FileTestHelper.generateRandomFileName()); final CloudFile upFile = new CloudFile(credentials.transformUri(uri)); upFile.upload(FileTestHelper.getRandomDataStream(512), 512, null, null, null); upFile.create(0); upFile.upload(FileTestHelper.getRandomDataStream(512), 512, null, null, null); fail(); file.downloadAttributes(); file.uploadMetadata();
String sasToken = copy.generateSharedAccessSignature(policy, null); CloudFile copySas = new CloudFile(new URI(copy.getUri().toString() + "?" + sasToken)); sasBlob.exists(); String copyId = copySas.startCopy(BlobTestHelper.defiddler(sasBlob)); FileTestHelper.waitForCopy(copySas); copySas.downloadAttributes(); FileProperties prop1 = copySas.getProperties(); BlobProperties prop2 = sasBlob.getProperties(); assertEquals(prop1.getContentType(), prop2.getContentType()); assertEquals("value", copySas.getMetadata().get("Test")); assertEquals(copyId, copySas.getCopyState().getCopyId()); copySas.delete(); container.delete();
file.create(1024); fail("Shouldn't get here"); file.delete(); fail("Shouldn't get here"); file.uploadMetadata(); fail("Shouldn't get here"); file.abortCopy(null); fail("Shouldn't get here"); file.clearRange(0, 512); fail("Shouldn't get here"); file.startCopy(file); fail("Shouldn't get here"); file.upload(null, 512); fail("Shouldn't get here");
/** * 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()); }
sasFile.create(length); file = share.getRootDirectoryReference().getFileReference(sasFile.getName()); sasFile.create(length); fail(); assertMessagesMatch(AccountSasTests.INVALID_PERMISSION_MESSAGE, ex); file = FileTestHelper.uploadNewFile(share, FileTestHelper.getRandomDataStream(0), length, null); sasFile = sasShare.getRootDirectoryReference().getFileReference(file.getName()); assertTrue(file.exists()); sasFile.upload(sourcestream, length); sasFile.upload(sourcestream, length); fail(); file.upload(sourcestream, length); sasFile.download(outStream); sasFile.download(outStream); fail(); file.download(outStream); sasFile.delete(); sasFile.delete();
file.create(1024); String eTag = file.getProperties().getEtag(); Date lastModified = file.getProperties().getLastModified(); file.getProperties().setCacheControl("no-transform"); file.getProperties().setContentDisposition("attachment"); file.getProperties().setContentEncoding("gzip"); file.getProperties().setContentLanguage("tr,en"); file.getProperties().setContentMD5("MDAwMDAwMDA="); file.getProperties().setContentType("text/html"); file.uploadProperties(); assertTrue(file.getProperties().getLastModified().after(lastModified)); assertTrue(!eTag.equals(file.getProperties().getEtag())); file2.downloadAttributes(); assertEquals("no-transform", file2.getProperties().getCacheControl()); assertEquals("attachment", file2.getProperties().getContentDisposition()); assertEquals("gzip", file2.getProperties().getContentEncoding()); assertEquals("tr,en", file2.getProperties().getContentLanguage()); assertEquals("MDAwMDAwMDA=", file2.getProperties().getContentMD5()); assertEquals("text/html", file2.getProperties().getContentType()); FileRequestOptions options = new FileRequestOptions(); options.setDisableContentMD5Validation(true); file3.download(stream, null, options, null); FileTestHelper.assertAreEqual(file2.getProperties(), file3.getProperties()); assertEquals(file2.getProperties().getLength(), file4.getProperties().getLength());
CloudFile fileRef1 = this.share.getRootDirectoryReference().getFileReference(fileName1); fileRef1.upload(FileTestHelper.getRandomDataStream(length), length); FileProperties props1 = fileRef1.getProperties(); fileRef1.uploadProperties(); fileRef1.downloadAttributes(null, options, null); FileProperties props2 = fileRef1.getProperties(); props1 = fileRef1.getProperties(); fileRef1.upload(FileTestHelper.getRandomDataStream(length), length); fileRef1.download(new ByteArrayOutputStream(), null, options, null); props2 = fileRef1.getProperties();
/** * Test file constructor. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileConstructor() throws URISyntaxException, StorageException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file1"); CloudFile file2 = new CloudFile(file.getStorageUri(), file.getServiceClient().getCredentials()); assertEquals(file.getName(), file2.getName()); assertEquals(file.getStorageUri(), file2.getStorageUri()); assertEquals(file.getShare().getStorageUri(), file2.getShare().getStorageUri()); assertEquals(FileTestHelper.ensureTrailingSlash(file.getServiceClient().getStorageUri()), FileTestHelper.ensureTrailingSlash(file2.getServiceClient().getStorageUri())); CloudFile file3 = new CloudFile(file2); assertEquals(file3.getName(), file2.getName()); assertEquals(file3.getStorageUri(), file2.getStorageUri()); assertEquals(file3.getShare().getStorageUri(), file2.getShare().getStorageUri()); assertEquals(FileTestHelper.ensureTrailingSlash(file3.getServiceClient().getStorageUri()), FileTestHelper.ensureTrailingSlash(file2.getServiceClient().getStorageUri())); }