-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1604 from SanjalKatiyar/actions_presigned_url
Add object actions - presigned URL, download and preview
- Loading branch information
Showing
13 changed files
with
464 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/odf/components/s3-browser/download-and-preview/download-and-preview.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { GetObjectCommandOutput } from '@aws-sdk/client-s3'; | ||
import { S3Commands } from '@odf/shared/s3'; | ||
import { getName } from '@odf/shared/selectors'; | ||
import { ObjectCrFormat } from '../../../types'; | ||
|
||
type DownloadAndPreviewFunction = ( | ||
bucketName: string, | ||
object: ObjectCrFormat, | ||
noobaaS3: S3Commands, | ||
setDownloadAndPreview: React.Dispatch< | ||
React.SetStateAction<DownloadAndPreviewState> | ||
> | ||
) => void; | ||
|
||
type GetObjectURL = ( | ||
bucketName: string, | ||
object: ObjectCrFormat, | ||
noobaaS3: S3Commands | ||
) => Promise<string>; | ||
|
||
export type DownloadAndPreviewState = { | ||
isDownloading: boolean; | ||
isPreviewing: boolean; | ||
}; | ||
|
||
const getObjectURL: GetObjectURL = async (bucketName, object, noobaaS3) => { | ||
const responseStream: GetObjectCommandOutput = await noobaaS3.getObject({ | ||
Bucket: bucketName, | ||
Key: getName(object), | ||
}); | ||
const blob = await new Response(responseStream.Body as ReadableStream).blob(); | ||
|
||
return window.URL.createObjectURL(blob); | ||
}; | ||
|
||
export const onDownload: DownloadAndPreviewFunction = async ( | ||
bucketName, | ||
object, | ||
noobaaS3, | ||
setDownloadAndPreview | ||
) => { | ||
try { | ||
setDownloadAndPreview((downloadAndPreview) => ({ | ||
...downloadAndPreview, | ||
isDownloading: true, | ||
})); | ||
|
||
const objectURL = await getObjectURL(bucketName, object, noobaaS3); | ||
|
||
// create a download element and trigger download | ||
const downloadLink = document.createElement('a'); | ||
downloadLink.href = objectURL; | ||
downloadLink.download = getName(object); | ||
document.body.appendChild(downloadLink); | ||
downloadLink.click(); | ||
document.body.removeChild(downloadLink); | ||
|
||
window.URL.revokeObjectURL(objectURL); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error fetching S3 object:', err); | ||
} finally { | ||
setDownloadAndPreview((downloadAndPreview) => ({ | ||
...downloadAndPreview, | ||
isDownloading: false, | ||
})); | ||
} | ||
}; | ||
|
||
export const onPreview: DownloadAndPreviewFunction = async ( | ||
bucketName, | ||
object, | ||
noobaaS3, | ||
setDownloadAndPreview | ||
) => { | ||
try { | ||
setDownloadAndPreview((downloadAndPreview) => ({ | ||
...downloadAndPreview, | ||
isPreviewing: true, | ||
})); | ||
|
||
const objectURL = await getObjectURL(bucketName, object, noobaaS3); | ||
|
||
// open the object URL in a new browser tab | ||
window.open(objectURL, '_blank'); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error fetching S3 object:', err); | ||
} finally { | ||
setDownloadAndPreview((downloadAndPreview) => ({ | ||
...downloadAndPreview, | ||
isPreviewing: false, | ||
})); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.