The aforementioned function will attempt to upload the file using your selected driver and template and will return a String representing the location of the file.
If you want to upload an image named profile.png your call site would look like:
try Storage.upload(
bytes: bytes,
fileName: "profile.png",
on: req
)
Base64 and data URI 📡
Is your file a base64 or data URI? No problem!
Storage.upload(base64: "SGVsbG8sIFdvcmxkIQ==", fileName: "base64.txt", on: req)
Storage.upload(dataURI: "data:,Hello%2C%20World!", fileName: "data-uri.txt", on: req)
Remote resources
Download an asset from a URL and then reupload it to your storage server.
Storage.upload(url: "http://mysite.com/myimage.png", fileName: "profile.png", on: req)
Download a file ✅
To download a file that was previously uploaded you simply use the generated path.
// download image as `Foundation.Data`
let data = try Storage.get("/images/profile.png", on: req)
Get CDN path
In order to use the CDN path convenience, you’ll have to set the CDN base url on Storage, e.g. in your configure.swift file:
Storage.cdnBaseURL = "https://cdn.vapor.cloud"
Here is how you generate the CDN path to a given asset.
let cdnPath = try Storage.getCDNPath(for: path)
If your CDN path is more involved than cdnUrl + path, you can build out Storage’s optional completionhandler to override the default functionality.
Storage.cdnPathBuilder = { baseURL, path in
let joinedPath = (baseURL + path)
return joinedPath.replacingOccurrences(of: "/images/original/", with: "/image/")
}
Delete a file ❌
Deleting a file can be done as follows.
try Storage.delete("/images/profile.png")
Configuration ⚙
Storage has a variety of configurable options.
Network driver 🔨
The network driver is the module responsible for interacting with your 3rd party service. The default, and currently the only, driver is s3.
bucket, accessKeyand secretKey are required by the S3 driver, while template, host and region are optional. region will default to eu-west-1 and host will default to s3.amazonaws.com.
Upload path 🛣
A times, you may need to upload files to a different scheme than /file.ext. You can achieve this by passing in the pathTemplate parameter when creating the S3Driver. If the parameter is omitted it will default to /#file.
The following template will upload profile.png from the folder images to /myapp/images/profile.png
Storage 🗄
A package to ease the use of multiple storage and CDN services.
Table of Contents
📦 Installation
Add
Storage
to the package dependencies (in yourPackage.swift
file):as well as to your target (e.g. “App”):
Getting started 🚀
Storage makes it easy to start uploading and downloading files. Just register a network driver and get going.
Upload a file 🌐
There are a few different interfaces for uploading a file, the simplest being the following:
The aforementioned function will attempt to upload the file using your selected driver and template and will return a
String
representing the location of the file.If you want to upload an image named
profile.png
your call site would look like:Base64 and data URI 📡
Is your file a base64 or data URI? No problem!
Remote resources
Download an asset from a URL and then reupload it to your storage server.
Download a file ✅
To download a file that was previously uploaded you simply use the generated path.
Get CDN path
In order to use the CDN path convenience, you’ll have to set the CDN base url on Storage, e.g. in your
configure.swift
file:Here is how you generate the CDN path to a given asset.
If your CDN path is more involved than
cdnUrl
+path
, you can build out Storage’s optional completionhandler to override the default functionality.Delete a file ❌
Deleting a file can be done as follows.
Configuration ⚙
Storage
has a variety of configurable options.Network driver 🔨
The network driver is the module responsible for interacting with your 3rd party service. The default, and currently the only, driver is
s3
.bucket
,accessKey
andsecretKey
are required by the S3 driver, whiletemplate
,host
andregion
are optional.region
will default toeu-west-1
andhost
will default tos3.amazonaws.com
.Upload path 🛣
A times, you may need to upload files to a different scheme than
/file.ext
. You can achieve this by passing in thepathTemplate
parameter when creating theS3Driver
. If the parameter is omitted it will default to/#file
.The following template will upload
profile.png
from the folderimages
to/myapp/images/profile.png
Aliases
Aliases are special keys in your template that will be replaced with dynamic information at the time of upload.
Note: if you use an alias and the information wasn’t provided at the file upload’s callsite, Storage will throw a
missingX
/malformedX
error.#file
: The file’s name and extension.#fileName
: The file’s name.#fileExtension
: The file’s extension.#folder
: The provided folder.#mime
: The file’s content type.#mimeFolder
: A folder generated according to the file’s mime.This alias will check the file’s mime and if it’s an image, it will return
images/original
else it will returndata
#day
: The current day.#month
: The current month.#year
: The current year.#timestamp
: The time of upload.#uuid
: A generated UUID.🏆 Credits
This package is developed and maintained by the Vapor team at Nodes.
📄 License
This package is open-sourced software licensed under the MIT license