Files
medusa-store/www/docs/content/plugins/file-service/spaces.md
Shahed Nasser 914d773d3a api-ref: custom API reference (#4770)
* initialized next.js project

* finished markdown sections

* added operation schema component

* change page metadata

* eslint fixes

* fixes related to deployment

* added response schema

* resolve max stack issue

* support for different property types

* added support for property types

* added loading for components

* added more loading

* type fixes

* added oneOf type

* removed console

* fix replace with push

* refactored everything

* use static content for description

* fixes and improvements

* added code examples section

* fix path name

* optimizations

* fixed tag navigation

* add support for admin and store references

* general enhancements

* optimizations and fixes

* fixes and enhancements

* added search bar

* loading enhancements

* added loading

* added code blocks

* added margin top

* add empty response text

* fixed oneOf parameters

* added path and query parameters

* general fixes

* added base path env variable

* small fix for arrays

* enhancements

* design enhancements

* general enhancements

* fix isRequired

* added enum values

* enhancements

* general fixes

* general fixes

* changed oas generation script

* additions to the introduction section

* added copy button for code + other enhancements

* fix response code block

* fix metadata

* formatted store introduction

* move sidebar logic to Tags component

* added test env variables

* fix code block bug

* added loading animation

* added expand param + loading

* enhance operation loading

* made responsive + improvements

* added loading provider

* fixed loading

* adjustments for small devices

* added sidebar label for endpoints

* added feedback component

* fixed analytics

* general fixes

* listen to scroll for other headings

* added sample env file

* update api ref files + support new fields

* fix for external docs link

* added new sections

* fix last item in sidebar not showing

* move docs content to www/docs

* change redirect url

* revert change

* resolve build errors

* configure rewrites

* changed to environment variable url

* revert changing environment variable name

* add environment variable for API path

* fix links

* fix tailwind settings

* remove vercel file

* reconfigured api route

* move api page under api

* fix page metadata

* fix external link in navigation bar

* update api spec

* updated api specs

* fixed google lint error

* add max-height on request samples

* add padding before loading

* fix for one of name

* fix undefined types

* general fixes

* remove response schema example

* redesigned navigation bar

* redesigned sidebar

* fixed up paddings

* added feedback component + report issue

* fixed up typography, padding, and general styling

* redesigned code blocks

* optimization

* added error timeout

* fixes

* added indexing with algolia + fixes

* fix errors with algolia script

* redesign operation sections

* fix heading scroll

* design fixes

* fix padding

* fix padding + scroll issues

* fix scroll issues

* improve scroll performance

* fixes for safari

* optimization and fixes

* fixes to docs + details animation

* padding fixes for code block

* added tab animation

* fixed incorrect link

* added selection styling

* fix lint errors

* redesigned details component

* added detailed feedback form

* api reference fixes

* fix tabs

* upgrade + fixes

* updated documentation links

* optimizations to sidebar items

* fix spacing in sidebar item

* optimizations and fixes

* fix endpoint path styling

* remove margin

* final fixes

* change margin on small devices

* generated OAS

* fixes for mobile

* added feedback modal

* optimize dark mode button

* fixed color mode useeffect

* minimize dom size

* use new style system

* radius and spacing design system

* design fixes

* fix eslint errors

* added meta files

* change cron schedule

* fix docusaurus configurations

* added operating system to feedback data

* change content directory name

* fixes to contribution guidelines

* revert renaming content

* added api-reference to documentation workflow

* fixes for search

* added dark mode + fixes

* oas fixes

* handle bugs

* added code examples for clients

* changed tooltip text

* change authentication to card

* change page title based on selected section

* redesigned mobile navbar

* fix icon colors

* fix key colors

* fix medusa-js installation command

* change external regex in algolia

* change changeset

* fix padding on mobile

* fix hydration error

* update depedencies
2023-08-15 18:07:54 +03:00

7.6 KiB
Raw Blame History

description, addHowToData
description addHowToData
Learn how to integrate Spaces with the Medusa backend. Learn how to install and configure the Spaces plugin on the Medusa backend. true

Spaces

In this document, youll learn how to install the Spaces plugin on your Medusa backend and use it for storage.

Overview

To manage images in Medusa, you need a file service plugin responsible for hosting the images. Without a file service plugin, you will face issues while working with Medusa, such as when uploading images for products.

Medusa provides three different options to handle your file storage. This document focuses on using Spaces to store images and files uploaded to the Medusa backend.


Prerequisites

Medusa Backend

A Medusa backend is required to be set up before following along with this document. You can follow the quickstart guide to get started in minutes.

Required Accounts

You need to create a DigitalOcean account to follow along with this documentation. A credit card is required during registration.


Create DigitalOcean Space

In your DigitalOcean account, click on the Create button at the top right, then choose Spaces from the dropdown.

Click on the green Create button at the top right, then choose Spaces in the dropdown

In the Create a Space form, you can choose any of the regions listed. You can alternatively leave all settings as they are and scroll down to the Finalize and Create section.

The Space form can be left as is and move towards the Finalize and Create section

In the Finalize and Create section, enter a name for the field “Choose a unique name”. Youll use this name later in the integration with Medusa. Also, select the project you want to add the new Space to.

Enter a name for the Space in the Choose a unique name field

Once youre done, click on the Create a Space button. This creates the Space and redirects you to the Spaces page.


Create Space Access Keys

Choose API from the bottom of the sidebar.

Choose API from the bottom of the sidebar.

This opens the Application & API page. Scroll down to Spaces Access Keys and click the Generate New Key button.

In the Spaces access keys section, click the Generate New Key button

This shows a table with the Name field editable. Enter a name for the Access Keys and click on the checkmark button to save and generate the Spaces access keys.

Enter a name for the Space access key in the field under the Name column

Then, two keys will be available under the Key column of the table. The first one is the Access Key ID and the second is the Secret Access Key. Copy both as youll use them later.

:::caution

The secret access key will not be shown again after you leave the page. Make sure to copy it when you see it or youll need to re-generate a new one.

:::


Install the Spaces Plugin

In the directory of your Medusa backend, run the following command to install the Spaces plugin:

npm install medusa-file-spaces

Then, add the following environment variables:

SPACE_URL=<YOUR_SPACE_URL>
SPACE_BUCKET=<YOUR_SPACE_NAME>
SPACE_ENDPOINT=<YOUR_SPACE_ENDPOINT>
SPACE_ACCESS_KEY_ID=<YOUR_ACCESS_KEY_ID>
SPACE_SECRET_ACCESS_KEY=<YOUR_SECRET_ACCESS_KEY>

Where:

  1. <YOUR_SPACE_URL> is the URL of your Space which you can find on the Spaces page below the Spaces name.

The URL is available under the Space's name on the Space's page

  1. <YOUR_SPACE_NAME> is the name of your Space.
  2. <YOUR_SPACE_ENDPOINT> is your Spaces endpoint which can be found by going to your Spaces page, clicking on the Settings tab, and scrolling to the Endpoint section.

Endpoint can be found on the Settings tab in the Space's page

  1. <YOUR_ACCESS_KEY_ID> and <YOUR_SECRET_ACCESS_KEY> are the keys you created in the previous section.

Finally, in medusa-config.js add a new item to the plugins array:

const plugins = [
  // ...
  {
    resolve: `medusa-file-spaces`,
    options: {
        spaces_url: process.env.SPACE_URL,
        bucket: process.env.SPACE_BUCKET,
        endpoint: process.env.SPACE_ENDPOINT,
        access_key_id: process.env.SPACE_ACCESS_KEY_ID,
        secret_access_key: process.env.SPACE_SECRET_ACCESS_KEY,
    },
  },
]

:::caution

If you have multiple storage plugins configured, the last plugin declared in the medusa-config.js file will be used.

:::


Test the Space Plugin

Run your Medusa backend with the following command:

npx medusa develop

Then, you can either test the plugin using the REST APIs or using the Medusa Admin.

On the Medusa Admin, create a new product and, in the Images section, upload an image then click Save. If the integration was successful, the product image will be uploaded successfully.

Image successfully uploaded on Medusa's admin

You can also check that the image was uploaded on the Spaces page.

Image can be seen on the Space's page on DigitalOcean


Next.js Starter Template Configuration

If youre using a Next.js Starter Template, you need to add an additional configuration that adds the Spaces domain name into the configured images domain names. This is because all URLs of product images will be from the Space.

If this configuration is not added, youll receive the error "next/image Un-configured Host”.

In next.config.js add the following option in the exported object:

const { withStoreConfig } = require("./store-config")

// ...

module.exports = withStoreConfig({
  // ...
  images: {
    domains: [
      // ...
      "<YOUR_SPACE_DOMAIN>",
    ],
  },
})

Where <YOUR_SPACE_DOMAIN> is the domain name for your Space which can be retrieved from the Space URL. For example, medusa-backend.digitaloceanspaces.com.


See Also