Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage protected level #780

Merged
merged 5 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/media/storage_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,23 @@ Storage.get('test.txt')
.catch(err => console.log(err));
```

Protected bucket:
To get current user's objects
```js
Storage.get('test.txt', { level: 'protected' })
.then(result => console.log(result))
.catch(err => console.log(err));
```
To get other users' objects
```js
Storage.get('test.txt', {
level: 'protected',
identityId: 'xxxxxxx' // the identityId of that user
})
.then(result => console.log(result))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add extra space to be consistent with the other example

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other methods didn't have an example: put, list and remove.

Maybe another use case could be listing on protected prefix (to read all the identityId under that prefix). We can discuss offline

.catch(err => console.log(err));
```

Private bucket:
```js
Storage.get('test.txt', {level: 'private'})
Expand Down
19 changes: 19 additions & 0 deletions packages/aws-amplify/__tests__/Storage/Storage-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,25 @@ describe('Storage', () => {
curCredSpyOn.mockClear();
});

test('get object with identityId option', async () => {
const curCredSpyOn = jest.spyOn(Auth.prototype, 'currentCredentials')
.mockImplementationOnce(() => {
return new Promise((res, rej) => {
res({});
});
});

const storage = new Storage(options);
const spyon = jest.spyOn(S3.prototype, 'getSignedUrl');

expect.assertions(2);
expect(await storage.get('key', { level: 'protected', identityId: 'identityId' })).toBe('url');
expect(spyon).toBeCalledWith('getObject', {"Bucket": "bucket", "Key": "protected/identityId/key" });

spyon.mockClear();
curCredSpyOn.mockClear();
});

test('credentials not ok', async () => {
const curCredSpyOn = jest.spyOn(Auth.prototype, 'currentCredentials')
.mockImplementationOnce(() => {
Expand Down
5 changes: 3 additions & 2 deletions packages/aws-amplify/src/Storage/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,9 @@ export default class StorageClass {
const { credentials, level } = options;

const customPrefix = options.customPrefix || {};
const privatePath = (customPrefix.private || 'private/') + credentials.identityId + '/';
const protectedPath = (customPrefix.protected || 'protected/') + credentials.identityId + '/';
const identityId = options.identityId || credentials.identityId;
const privatePath = (customPrefix.private || 'private/') + identityId + '/';
const protectedPath = (customPrefix.protected || 'protected/') + identityId + '/';
const publicPath = customPrefix.public || 'public/';

switch (level) {
Expand Down