/** * Returns a shared access signature for the file using the specified group policy identifier and * shared access file headers. Note this does not contain the leading "?". * * @param policy * A <code>{@link SharedAccessFilePolicy}</code> object that represents the access policy for the shared * access signature. * @param groupPolicyIdentifier * A <code>String</code> that represents the share-level access policy. * * @return A <code>String</code> that represents the shared access signature. * * @throws InvalidKeyException * If the credentials are invalid. * @throws StorageException * If a storage service error occurred. */ public String generateSharedAccessSignature(final SharedAccessFilePolicy policy, final String groupPolicyIdentifier) throws InvalidKeyException, StorageException { return generateSharedAccessSignature(policy, null /* headers */, groupPolicyIdentifier); }
/** * Returns a shared access signature for the file using the specified group policy identifier and * shared access file headers. Note this does not contain the leading "?". * * @param policy * A <code>{@link SharedAccessFilePolicy}</code> object that represents the access policy for the shared * access signature. * @param groupPolicyIdentifier * A <code>String</code> that represents the share-level access policy. * * @return A <code>String</code> that represents the shared access signature. * * @throws InvalidKeyException * If the credentials are invalid. * @throws StorageException * If a storage service error occurred. */ public String generateSharedAccessSignature(final SharedAccessFilePolicy policy, final String groupPolicyIdentifier) throws InvalidKeyException, StorageException { return generateSharedAccessSignature(policy, null /* headers */, groupPolicyIdentifier); }
/** * Returns a shared access signature for the file using the specified group policy identifier and * shared access file headers. Note this does not contain the leading "?". * * @param policy * A <code>{@link SharedAccessFilePolicy}</code> object that represents the access policy for the shared * access signature. * @param headers * A <code>{@link SharedAccessFileHeaders}</code> object that represents the optional header values to * set for a file accessed with this shared access signature. * @param groupPolicyIdentifier * A <code>String</code> that represents the share-level access policy. * * @return A <code>String</code> that represents the shared access signature. * * @throws IllegalArgumentException * If the credentials are invalid. * @throws InvalidKeyException * If the credentials are invalid. * @throws StorageException * If a storage service error occurred. */ public String generateSharedAccessSignature( final SharedAccessFilePolicy policy, final SharedAccessFileHeaders headers, final String groupPolicyIdentifier) throws InvalidKeyException, StorageException { return this.generateSharedAccessSignature(policy, headers, groupPolicyIdentifier, null /* IP range */, null /* protocols */); }
/** * Returns a shared access signature for the file using the specified group policy identifier and * shared access file headers. Note this does not contain the leading "?". * * @param policy * A <code>{@link SharedAccessFilePolicy}</code> object that represents the access policy for the shared * access signature. * @param headers * A <code>{@link SharedAccessFileHeaders}</code> object that represents the optional header values to * set for a file accessed with this shared access signature. * @param groupPolicyIdentifier * A <code>String</code> that represents the share-level access policy. * * @return A <code>String</code> that represents the shared access signature. * * @throws IllegalArgumentException * If the credentials are invalid. * @throws InvalidKeyException * If the credentials are invalid. * @throws StorageException * If a storage service error occurred. */ public String generateSharedAccessSignature( final SharedAccessFilePolicy policy, final SharedAccessFileHeaders headers, final String groupPolicyIdentifier) throws InvalidKeyException, StorageException { return this.generateSharedAccessSignature(policy, headers, groupPolicyIdentifier, null /* IP range */, null /* protocols */); }
@Test public void testApiVersion() throws InvalidKeyException, StorageException, URISyntaxException { SharedAccessFilePolicy policy = createSharedAccessPolicy( EnumSet.of(SharedAccessFilePermissions.READ, SharedAccessFilePermissions.WRITE, SharedAccessFilePermissions.LIST, SharedAccessFilePermissions.DELETE), 300); String sas = this.file.generateSharedAccessSignature(policy, null); // should not be appended before signing assertEquals(-1, sas.indexOf(Constants.QueryConstants.API_VERSION)); OperationContext ctx = new OperationContext(); ctx.getResponseReceivedEventHandler().addListener(new StorageEvent<ResponseReceivedEvent>() { @Override public void eventOccurred(ResponseReceivedEvent eventArg) { // should be appended after signing HttpURLConnection conn = (HttpURLConnection) eventArg.getConnectionObject(); assertTrue(conn.getURL().toString().indexOf(Constants.QueryConstants.API_VERSION) != -1); } }); CloudFile sasFile = new CloudFile(new URI(this.file.getUri().toString() + "?" + sas)); sasFile.uploadMetadata(null, null, ctx); }
@Test public void testFileSASCombinations() throws URISyntaxException, StorageException, InvalidKeyException, IOException { EnumSet<SharedAccessFilePermissions> permissionSet = null; for (int bits = 0x1; bits < 0x10; bits++) { permissionSet = getPermissions(bits); SharedAccessFilePolicy policy = createSharedAccessPolicy(permissionSet, 300); CloudFile testFile = FileTestHelper.uploadNewFile(this.share, 512, null); String sasToken = testFile.generateSharedAccessSignature(policy, null, null); testAccess(sasToken, permissionSet, null, testFile); } }
@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()); }
new URI(this.file.getUri().toString() + "?" + this.file.generateSharedAccessSignature(null, headers, "rwperm"))); OperationContext context = new OperationContext();
@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(); }
cal.add(Calendar.MINUTE, 5); policy.setSharedAccessExpiryTime(cal.getTime()); String sasToken = source.generateSharedAccessSignature(policy, null, null);
policy.setSharedAccessExpiryTime(cal.getTime()); String sasToken = source.generateSharedAccessSignature(policy, null, null); policy.setSharedAccessExpiryTime(cal.getTime()); String sasToken = destination.generateSharedAccessSignature(policy, null, null);
policy.setSharedAccessExpiryTime(calendar.getTime()); String sasToken = source.generateSharedAccessSignature(policy, null, null);
policy.setSharedAccessExpiryTime(cal.getTime()); String sasToken = source.generateSharedAccessSignature(policy, null, null);
String sasToken = copy.generateSharedAccessSignature(policy, null); CloudFile copySas = new CloudFile(new URI(copy.getUri().toString() + "?" + sasToken));