File Sharing API for PubNub Go SDK

Allows users to upload and share files. You can upload any file of up to 5 MB in size. This feature is commonly used in social apps to share images, or in medical apps to share medical records for patients.

When a file is uploaded on a channel, it's stored and managed using a storage service, and associated with your key. Subscribers to that channel receive a file event which contains a file ID, filename, and optional description.

Send file

Description

Upload file / data to specified channel.

Method(s)

pn.SendFile().Channel(string).Message(string).CipherKey(string).Name(string).File(*os.File).TTL(int).ShouldStore(bool).Meta(interface{}).Execute()
ParameterTypeRequiredDefaultDescription
ChannelstringYesChannel to upload the file.
MessagestringNoMessage which should be sent along with file to specified channel.
File*os.FileYesPointer to the File object
CipherKeystringNoKey to be used to encrypt uploaded data. If is it not provided the CipherKey from the PNConfiguration will be used, if available.
TTLintNoHow long message should be stored in channel's storage.
ShouldStoreboolNotrueIf true the published file message will be stored in channel's history.
Metainterface{}NonullMeta data object which can be used with the filtering ability

Basic Usage

file, err := os.Open("cat_picture.jpg")
defer file.Close()
if err != nil {
panic(err)
}
resSendFile, statusSendFile, errSendFile := pn.SendFile().Channel("my_channel").Message("Look at this photo!").Name("cat_picture.jpg").File(file).Execute()
fmt.Println(resSendFile, statusSendFile, errSendFile)
fmt.Println(resSendFile.Data.ID)

Returns

The SendFile() operation returns a type PNSendFileResponse which contains the following properties:

Property NameTypeDescription
DataPNFileDataDetails of type PNFileData are here
Timestampint64Returns an int64 representation of the timetoken when the message was published.

PNFileData

PNFileData contains the following properties:

Property NameTypeDescription
IDstringReturns the ID of the file.

List channel files

Description

Retrieve list of files uploaded to Channel.

Method(s)

pn.ListFiles().Channel(string).Limit(int).Next(string).Execute()
ParameterTypeRequiredDefaultDescription
ChannelstringYesChannel to get list of files.
LimitintOptional100Number of files to return.
NextstringOptionalPreviously-returned cursor bookmark for fetching the next page.

Basic Usage

resListFile, statusListFile, errListFile := pn.ListFiles().Channel("my_channel").Execute()
fmt.Println(resListFile, statusListFile, errListFile)
if resListFile != nil {
for _, m := range resListFile.Data {
fmt.Println(m.ID, m.Created, m.Name, m.Size)
}
}

Returns

The ListFiles() operation returns a type PNListFilesResponse which contains the following properties:

Property NameTypeDescription
DataPNFileInfoDetails of type PNFileData are here
CountintNumber of files returned.
NextstringCursor bookmark for fetching the next page.

PNFileInfo

PNFileInfo contains the following properties:

Property NameTypeDescription
NamestringName of the file.
IdstringID of the file.
SizeintSize of the file.
CreatedstringCreated date of the file.

Get File Url

Description

Generate URL which can be used to download file from target Channel.

Method(s)

pn.GetFileURL().Channel(string).ID(string).Name(string).Execute()
ParameterTypeRequiredDescription
ChannelstringYesName of channel within which file with name has been uploaded.
IDstringYesUnique file identifier which has been assigned during file upload.
NamestringYesName under which the uploaded file is stored for the channel.

Basic Usage

resGetFileUrl, statusGetFileUrl, errGetFileUrl := pn.GetFileURL().Channel("my_channel").ID("d9515cb7-48a7-41a4-9284-f4bf331bc770").Name("cat_picture.jpg").Execute()
fmt.Println(resGetFileUrl, statusGetFileUrl, errGetFileUrl)
fmt.Println(resGetFileUrl.URL)

Returns

The GetFileUrl() operation returns a type PNGetFileURLResponse which contains the following properties:

Property NameTypeDescription
UrlstringURL which can be used to download remote file with specified name and identifier.

Download file

Description

Download file from specified Channel.

Method(s)

pn.DownloadFile().Channel(string).CipherKey(string).ID(string).Name(string).Execute()
ParameterTypeRequiredDescription
ChannelstringYesName of channel within which file with name has been uploaded.
IDstringYesUnique file identifier which has been assigned during file upload.
NamestringYesName under which uploaded file is stored for channel.
CipherKeystringOptionalKey which should be used to decrypt downloaded data. If CipherKey is not provided, SDK uses CipherKey of PNConfiguration.

Basic Usage

resDLFile, statusDLFile, errDLFile := pn.DownloadFile().Channel("my_channel").ID("d9515cb7-48a7-41a4-9284-f4bf331bc770").Name("cat_picture.jpg").Execute()
if resDLFile != nil {
filepathOutput := "cat_picture.jpg"
out, _ := os.Create(filepathOutput)
_, err := io.Copy(out, resDLFile.File)

if err != nil {
fmt.Println(err)
}
}

Returns

The DownloadFile() operation returns a type PNDownloadFileResponse which contains the following properties:

Property NameTypeDescription
Fileio.ReaderFile Reader that can be used to save the File.

Delete file

Description

Delete file from specified Channel.

Method(s)

pn.DeleteFile().Channel(string).ID(string).Name(string).Execute()
ParameterTypeRequiredDescription
ChannelstringYesName of channel within which file with name needs to be deleted.
IDstringYesUnique file identifier of the file to be deleted.
NamestringYesName of the file to be deleted from the channel.

Basic Usage

_, statusDelFile, errDelFile := pn.DeleteFile().Channel("my_channel").ID("d9515cb7-48a7-41a4-9284-f4bf331bc770").Name("cat_picture.jpg").Execute()
fmt.Println(statusDelFile, errDelFile)

Returns

The DeleteFile() operation returns a type PNDeleteFileResponse which is nil.

Publish file message

Description

Publish file message from specified Channel.

Method(s)

pn.PublishFileMessage().TTL(int).Meta(interface{}).ShouldStore(bool).Channel(string).Message(PNPublishFileMessage).Execute()
ParameterTypeRequiredDefaultDescription
TTLintOptionalHow long message should be stored in channel's storage.
Metainterface{}OptionalMeta data object which can be used with the filtering ability.
ShouldStoreboolOptionaltrueStore in history.
ChannelstringYesName of channel to publish file message.
MessagePNPublishFileMessageYesThe payload should be of the type PNPublishFileMessage.

Basic Usage

m := PNPublishMessage{
Text: "Look at this photo!",
}

file := PNFileInfoForPublish{
ID: "d9515cb7-48a7-41a4-9284-f4bf331bc770",
Name: "cat_picture.jpg",
}

message := PNPublishFileMessage{
PNFile: file,
PNMessage: m,
}
resPubFile, pubFileResponseStatus, errPubFileResponse := pn.PublishFileMessage().Channel("my_channel").Message(message).Execute()
fmt.Println(resPubFile, pubFileResponseStatus, errPubFileResponse)

Returns

The PublishFileMessage() operation returns type PublishFileMessageResponse which contains the following properties:

Property NameTypeDescription
Timetokenint64Returns a long representation of the timetoken when the message was published.
Last updated on