Class File (5.10.0)

Inheritance

ServiceObject<File> >File

Package

@google-cloud/storage!

Constructors

(constructor)(bucket, name, options)

constructor(bucket:Bucket,name:string,options?:FileOptions);

Constructs a file object.

Parameters
NameDescription
bucketBucket

The Bucket instance this file is attached to.

namestring

The name of the remote file.

optionsFileOptions

Configuration options.

Properties

acl

acl:Acl;
Property Value
TypeDescription
Acl

bucket

bucket:Bucket;
Property Value
TypeDescription
Bucket

generation

generation?:number;
Property Value
TypeDescription
number

kmsKeyName

kmsKeyName?:string;
Property Value
TypeDescription
string

metadata

metadata:Metadata;
Property Value
TypeDescription
Metadata

name

name:string;
Property Value
TypeDescription
string

parent

parent:Bucket;
Property Value
TypeDescription
Bucket

signer

signer?:URLSigner;
Property Value
TypeDescription
URLSigner

storage

storage:Storage;
Property Value
TypeDescription
Storage

userProject

userProject?:string;
Property Value
TypeDescription
string

Methods

copy(destination, options)

copy(destination:string|Bucket|File,options?:CopyOptions):Promise<CopyResponse>;
Parameters
NameDescription
destinationstring |Bucket |File
optionsCopyOptions
Returns
TypeDescription
Promise<CopyResponse>

copy(destination, callback)

copy(destination:string|Bucket|File,callback:CopyCallback):void;
Parameters
NameDescription
destinationstring |Bucket |File
callbackCopyCallback
Returns
TypeDescription
void

copy(destination, options, callback)

copy(destination:string|Bucket|File,options:CopyOptions,callback:CopyCallback):void;
Parameters
NameDescription
destinationstring |Bucket |File
optionsCopyOptions
callbackCopyCallback
Returns
TypeDescription
void

createReadStream(options)

createReadStream(options?:CreateReadStreamOptions):Readable;

Create a readable stream to read the contents of the remote file. It can be piped to a writable stream or listened to for 'data' events to read a file's contents.

In the unlikely event there is a mismatch between what you downloaded and the version in your Bucket, your error handler will receive an error with code "CONTENT_DOWNLOAD_MISMATCH". If you receive this error, the best recourse is to try downloading the file again.

For faster crc32c computation, you must manually install [fast-crc32c](https://www.npmjs.com/package/fast-crc32c):

$ npm install --save fast-crc32c

NOTE: Readable streams will emit theend event when the file is fully downloaded.

Parameter
NameDescription
optionsCreateReadStreamOptions

Configuration options.

Returns
TypeDescription
Readable

{ReadableStream}

createResumableUpload(options)

createResumableUpload(options?:CreateResumableUploadOptions):Promise<CreateResumableUploadResponse>;
Parameter
NameDescription
optionsCreateResumableUploadOptions
Returns
TypeDescription
Promise<CreateResumableUploadResponse>

createResumableUpload(options, callback)

createResumableUpload(options:CreateResumableUploadOptions,callback:CreateResumableUploadCallback):void;
Parameters
NameDescription
optionsCreateResumableUploadOptions
callbackCreateResumableUploadCallback
Returns
TypeDescription
void

createResumableUpload(callback)

createResumableUpload(callback:CreateResumableUploadCallback):void;
Parameter
NameDescription
callbackCreateResumableUploadCallback
Returns
TypeDescription
void

createWriteStream(options)

createWriteStream(options?:CreateWriteStreamOptions):Writable;

Create a writable stream to overwrite the contents of the file in your bucket.

A File object can also be used to create files for the first time.

Resumable uploads are automatically enabled and must be shut off explicitly by settingoptions.resumable tofalse.

Resumable uploads require write access to the $HOME directory. Through [config-store](https://www.npmjs.com/package/configstore), some metadata is stored. By default, if the directory is not writable, we will fall back to a simple upload. However, if you explicitly request a resumable upload, and we cannot write to the config directory, we will return aResumableUploadError.

There is some overhead when using a resumable upload that can cause noticeable performance degradation while uploading a series of small files. When uploading files less than 10MB, it is recommended that the resumable feature is disabled.

For faster crc32c computation, you must manually install [fast-crc32c](https://www.npmjs.com/package/fast-crc32c):

$ npm install --save fast-crc32c

NOTE: Writable streams will emit thefinish event when the file is fully uploaded.

[Upload Options (Simple or Resumable)]https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload [Objects: insert API Documentation]https://cloud.google.com/storage/docs/json_api/v1/objects/insert

Parameter
NameDescription
optionsCreateWriteStreamOptions

Configuration options.

Returns
TypeDescription
Writable

{WritableStream}

deleteResumableCache()

deleteResumableCache():void;

Delete failed resumable upload file cache.

Resumable file upload cache the config file to restart upload in case of failure. In certain scenarios, the resumable upload will not works and upload file cache needs to be deleted to upload the same file.

Following are some of the scenarios.

Resumable file upload failed even though the file is successfully saved on the google storage and need to clean up a resumable file cache to update the same file.

Resumable file upload failed due to pre-condition (i.e generation number is not matched) and want to upload a same file with the new generation number.

Returns
TypeDescription
void

download(options)

download(options?:DownloadOptions):Promise<DownloadResponse>;
Parameter
NameDescription
optionsDownloadOptions
Returns
TypeDescription
Promise<DownloadResponse>

download(options, callback)

download(options:DownloadOptions,callback:DownloadCallback):void;
Parameters
NameDescription
optionsDownloadOptions
callbackDownloadCallback
Returns
TypeDescription
void

download(callback)

download(callback:DownloadCallback):void;
Parameter
NameDescription
callbackDownloadCallback
Returns
TypeDescription
void

generateSignedPostPolicyV2(options)

generateSignedPostPolicyV2(options:GenerateSignedPostPolicyV2Options):Promise<GenerateSignedPostPolicyV2Response>;
Parameter
NameDescription
optionsGenerateSignedPostPolicyV2Options
Returns
TypeDescription
Promise<GenerateSignedPostPolicyV2Response>

generateSignedPostPolicyV2(options, callback)

generateSignedPostPolicyV2(options:GenerateSignedPostPolicyV2Options,callback:GenerateSignedPostPolicyV2Callback):void;
Parameters
NameDescription
optionsGenerateSignedPostPolicyV2Options
callbackGenerateSignedPostPolicyV2Callback
Returns
TypeDescription
void

generateSignedPostPolicyV2(callback)

generateSignedPostPolicyV2(callback:GenerateSignedPostPolicyV2Callback):void;
Parameter
NameDescription
callbackGenerateSignedPostPolicyV2Callback
Returns
TypeDescription
void

generateSignedPostPolicyV4(options)

generateSignedPostPolicyV4(options:GenerateSignedPostPolicyV4Options):Promise<GenerateSignedPostPolicyV4Response>;
Parameter
NameDescription
optionsGenerateSignedPostPolicyV4Options
Returns
TypeDescription
Promise<GenerateSignedPostPolicyV4Response>

generateSignedPostPolicyV4(options, callback)

generateSignedPostPolicyV4(options:GenerateSignedPostPolicyV4Options,callback:GenerateSignedPostPolicyV4Callback):void;
Parameters
NameDescription
optionsGenerateSignedPostPolicyV4Options
callbackGenerateSignedPostPolicyV4Callback
Returns
TypeDescription
void

generateSignedPostPolicyV4(callback)

generateSignedPostPolicyV4(callback:GenerateSignedPostPolicyV4Callback):void;
Parameter
NameDescription
callbackGenerateSignedPostPolicyV4Callback
Returns
TypeDescription
void

getExpirationDate()

getExpirationDate():Promise<GetExpirationDateResponse>;
Returns
TypeDescription
Promise<GetExpirationDateResponse>

getExpirationDate(callback)

getExpirationDate(callback:GetExpirationDateCallback):void;
Parameter
NameDescription
callbackGetExpirationDateCallback
Returns
TypeDescription
void

getSignedPolicy(options)

getSignedPolicy(options:GetSignedPolicyOptions):Promise<GetSignedPolicyResponse>;
Parameter
NameDescription
optionsGetSignedPolicyOptions
Returns
TypeDescription
Promise<GetSignedPolicyResponse>

getSignedPolicy(options, callback)

getSignedPolicy(options:GetSignedPolicyOptions,callback:GetSignedPolicyCallback):void;
Parameters
NameDescription
optionsGetSignedPolicyOptions
callbackGetSignedPolicyCallback
Returns
TypeDescription
void

getSignedPolicy(callback)

getSignedPolicy(callback:GetSignedPolicyCallback):void;
Parameter
NameDescription
callbackGetSignedPolicyCallback
Returns
TypeDescription
void

getSignedUrl(cfg)

getSignedUrl(cfg:GetSignedUrlConfig):Promise<GetSignedUrlResponse>;
Parameter
NameDescription
cfgGetSignedUrlConfig
Returns
TypeDescription
Promise<GetSignedUrlResponse>

getSignedUrl(cfg, callback)

getSignedUrl(cfg:GetSignedUrlConfig,callback:GetSignedUrlCallback):void;
Parameters
NameDescription
cfgGetSignedUrlConfig
callbackGetSignedUrlCallback
Returns
TypeDescription
void

isPublic()

isPublic():Promise<IsPublicResponse>;
Returns
TypeDescription
Promise<IsPublicResponse>

isPublic(callback)

isPublic(callback:IsPublicCallback):void;
Parameter
NameDescription
callbackIsPublicCallback
Returns
TypeDescription
void

makePrivate(options)

makePrivate(options?:MakeFilePrivateOptions):Promise<MakeFilePrivateResponse>;
Parameter
NameDescription
optionsMakeFilePrivateOptions
Returns
TypeDescription
Promise<MakeFilePrivateResponse>

makePrivate(callback)

makePrivate(callback:MakeFilePrivateCallback):void;
Parameter
NameDescription
callbackMakeFilePrivateCallback
Returns
TypeDescription
void

makePrivate(options, callback)

makePrivate(options:MakeFilePrivateOptions,callback:MakeFilePrivateCallback):void;
Parameters
NameDescription
optionsMakeFilePrivateOptions
callbackMakeFilePrivateCallback
Returns
TypeDescription
void

makePublic()

makePublic():Promise<MakeFilePublicResponse>;
Returns
TypeDescription
Promise<MakeFilePublicResponse>

makePublic(callback)

makePublic(callback:MakeFilePublicCallback):void;
Parameter
NameDescription
callbackMakeFilePublicCallback
Returns
TypeDescription
void

move(destination, options)

move(destination:string|Bucket|File,options?:MoveOptions):Promise<MoveResponse>;
Parameters
NameDescription
destinationstring |Bucket |File
optionsMoveOptions
Returns
TypeDescription
Promise<MoveResponse>

move(destination, callback)

move(destination:string|Bucket|File,callback:MoveCallback):void;
Parameters
NameDescription
destinationstring |Bucket |File
callbackMoveCallback
Returns
TypeDescription
void

move(destination, options, callback)

move(destination:string|Bucket|File,options:MoveOptions,callback:MoveCallback):void;
Parameters
NameDescription
destinationstring |Bucket |File
optionsMoveOptions
callbackMoveCallback
Returns
TypeDescription
void

publicUrl()

publicUrl():string;

The public URL of this File Use to enable anonymous access via the returned URL.

Returns
TypeDescription
string

{string}

rename(destinationFile, options)

rename(destinationFile:string|File,options?:RenameOptions):Promise<RenameResponse>;
Parameters
NameDescription
destinationFilestring |File
optionsRenameOptions
Returns
TypeDescription
Promise<RenameResponse>

rename(destinationFile, callback)

rename(destinationFile:string|File,callback:RenameCallback):void;
Parameters
NameDescription
destinationFilestring |File
callbackRenameCallback
Returns
TypeDescription
void

rename(destinationFile, options, callback)

rename(destinationFile:string|File,options:RenameOptions,callback:RenameCallback):void;
Parameters
NameDescription
destinationFilestring |File
optionsRenameOptions
callbackRenameCallback
Returns
TypeDescription
void

request(reqOpts)

request(reqOpts:DecorateRequestOptions):Promise<[ResponseBody,Metadata]>;
Parameter
NameDescription
reqOptsDecorateRequestOptions
Returns
TypeDescription
Promise<[ResponseBody,Metadata]>

request(reqOpts, callback)

request(reqOpts:DecorateRequestOptions,callback:BodyResponseCallback):void;
Parameters
NameDescription
reqOptsDecorateRequestOptions
callbackBodyResponseCallback
Returns
TypeDescription
void

rotateEncryptionKey(options)

rotateEncryptionKey(options?:RotateEncryptionKeyOptions):Promise<RotateEncryptionKeyResponse>;
Parameter
NameDescription
optionsRotateEncryptionKeyOptions
Returns
TypeDescription
Promise<RotateEncryptionKeyResponse>

rotateEncryptionKey(callback)

rotateEncryptionKey(callback:RotateEncryptionKeyCallback):void;
Parameter
NameDescription
callbackRotateEncryptionKeyCallback
Returns
TypeDescription
void

rotateEncryptionKey(options, callback)

rotateEncryptionKey(options:RotateEncryptionKeyOptions,callback:RotateEncryptionKeyCallback):void;
Parameters
NameDescription
optionsRotateEncryptionKeyOptions
callbackRotateEncryptionKeyCallback
Returns
TypeDescription
void

save(data, options)

save(data:string|Buffer,options?:SaveOptions):Promise<void>;
Parameters
NameDescription
datastring |Buffer
optionsSaveOptions
Returns
TypeDescription
Promise<void>

save(data, callback)

save(data:string|Buffer,callback:SaveCallback):void;
Parameters
NameDescription
datastring |Buffer
callbackSaveCallback
Returns
TypeDescription
void

save(data, options, callback)

save(data:string|Buffer,options:SaveOptions,callback:SaveCallback):void;
Parameters
NameDescription
datastring |Buffer
optionsSaveOptions
callbackSaveCallback
Returns
TypeDescription
void

setEncryptionKey(encryptionKey)

setEncryptionKey(encryptionKey:string|Buffer):this;

The Storage API allows you to use a custom key for server-side encryption.

[Customer-supplied Encryption Keys]https://cloud.google.com/storage/docs/encryption#customer-supplied

Parameter
NameDescription
encryptionKeystring |Buffer

An AES-256 encryption key.

Returns
TypeDescription
this

{File}

setStorageClass(storageClass, options)

setStorageClass(storageClass:string,options?:SetStorageClassOptions):Promise<SetStorageClassResponse>;
Parameters
NameDescription
storageClassstring
optionsSetStorageClassOptions
Returns
TypeDescription
Promise<SetStorageClassResponse>

setStorageClass(storageClass, options, callback)

setStorageClass(storageClass:string,options:SetStorageClassOptions,callback:SetStorageClassCallback):void;
Parameters
NameDescription
storageClassstring
optionsSetStorageClassOptions
callbackSetStorageClassCallback
Returns
TypeDescription
void

setStorageClass(storageClass, callback)

setStorageClass(storageClass:string,callback?:SetStorageClassCallback):void;
Parameters
NameDescription
storageClassstring
callbackSetStorageClassCallback
Returns
TypeDescription
void

setUserProject(userProject)

setUserProject(userProject:string):void;

Set a user project to be billed for all requests made from this File object.

Parameter
NameDescription
userProjectstring

The user project.

Returns
TypeDescription
void

startResumableUpload_(dup, options)

startResumableUpload_(dup:Duplexify,options:CreateResumableUploadOptions):void;

This creates a gcs-resumable-upload upload stream.

[gcs-resumable-upload]https://github.com/stephenplusplus/gcs-resumable-upload

Parameters
NameDescription
dupDuplexify
optionsCreateResumableUploadOptions

Configuration object.

Returns
TypeDescription
void

startSimpleUpload_(dup, options)

startSimpleUpload_(dup:Duplexify,options?:CreateWriteStreamOptions):void;

Takes a readable stream and pipes it to a remote file. UnlikestartResumableUpload_, which uses the resumable upload technique, this method uses a simple upload (all or nothing).

Parameters
NameDescription
dupDuplexify

Duplexify stream of data to pipe to the file.

optionsCreateWriteStreamOptions

Configuration object.

Returns
TypeDescription
void

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-30 UTC.