@textile/hub > Buckets > pushPath
Buckets.pushPath() method
Pushes a file to a bucket path.
Signature:
pushPath(key: string, path: string, input: any, options?: PushOptions): Promise<PushPathResult>;
Parameters
Parameter | Type | Description |
---|---|---|
key | string | Unique (IPNS compatible) identifier key for a bucket. |
path | string | A file/object (sub)-path within a bucket. |
input | any | The input file/stream/object. |
options | PushOptions |
Returns:
Promise<PushPathResult>
Remarks
- This will return the resolved path and the bucket's new root path. - If pushing NodeJS streams, ensure you set your highwatermark to an appropriate size (i.e., ~1024 bytes) for optimal behavior on slow or intermittent connections. See example below or use
utils.createReadStream
.
Example 1
Push a file to the root of a bucket
import { Buckets } from '@textile/hub'
const pushFile = async (buckets: Buckets, content: string, bucketKey: string) => {
const file = { path: '/index.html', content: Buffer.from(content) }
return await buckets.pushPath(bucketKey!, 'index.html', file)
}
Example 2
Push an folder in node.js
import fs from 'fs'
import util from 'util'
import glob from 'glob'
import { Buckets } from '@textile/hub'
const globDir = util.promisify(glob)
// expects an already setup buckets session using getOrCreate or withThread
const exists = async (buckets: Buckets, bucketKey: string, dir: string) => {
const files = await globDir('<dir glob options>')
return await Promise.all(files.map(async (file) => {
const filePath = dir + '/' + file
var content = fs.createReadStream(filePath, { highWaterMark: 1024 });
const upload = {
path: file,
content
}
return await buckets.pushPath(bucketKey, file, upload)
}))
}