Configura il caricamento di file e immagini su S3

So, you want to use S3 to handle image uploads? Here’s the definitive guide, but also see Configure an S3 compatible object storage provider for uploads to see how to configure your app.yml.

S3 registration

Head over to https://aws.amazon.com/free/ and click on Create a Free Account

During the create account process, make sure you provide payment information, otherwise you won’t be able to use S3. There’s no registration fee, you will only be charged for what you use, if you exceed the AWS Free Usage Tier.

Bucket

Go to S3 and click on Create bucket, then fill out the Bucket name. Remember this name because we’ll need it for the next step.

Name of your bucket

Select a Region. You should enter the location (eg. “EU (Frankfurt)”) that is nearest to your users for better performance.

Scroll down a little until you get to the Permissions panel.

  • When you set up the permissions, make sure that you allow public ACLs, otherwise uploads will fail. Uncheck Block all public access and check the bottom two checkboxes. You’ll also have to acknowledge that your settings may make the bucket public in the warning at the top.

User creation

Creating a policy

Sign in to AWS Management Console and search for the “IAM” service to access the AWS Identity and Access Management (IAM) console which enables you to manage access to your AWS resources.

First, click Policies in the sidebar. Then, click on Create Policy and choose the JSON tab:

image

Use the following piece of code as a template for your policy document:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
               "s3:List*",
               "s3:Get*",
               "s3:AbortMultipartUpload",
               "s3:DeleteObject",
               "s3:PutObject",
               "s3:PutObjectAcl",
               "s3:PutObjectVersionAcl",
               "s3:PutLifecycleConfiguration",
               "s3:CreateBucket",
               "s3:PutBucketCORS"
      ],
      "Resource": [
        "arn:aws:s3:::=BUCKET=",
        "arn:aws:s3:::=BUCKET=/*"
      ]
    },
    {
       "Effect": "Allow",
       "Action": [
           "s3:ListAllMyBuckets",
           "s3:ListBucket"
       ],
       "Resource": "*"
    }
  ]
}

:warning: Make sure that these two lines contain the actual name of your bucket. :blush:

image

:star2: If you also intend to do S3 backups, you can include your backup bucket here too like this:

image

Then click on Review Policy and fill out the Name field. For example, you can call it s3-discourse-policy

Then click on Create Policy at the bottom.

Creating a user account

Now that you’ve created the right policy, you’re ready to create the user account. Click on the Users link on the left side of the IAM console and then the Add user button.

Type in a descriptive user name and make sure the “Programmatic access” checkbox is checked.

Setting permissions

The next step in the process is to configure the user’s permissions. Click on the button that says Next: Permissions and then click on «Attach existing policies directly»:
image

Now search for the policy name you created in the previous step (in our case it’s s3-discourse-policy). When you find it, select the checkbox and click on Next: Tags, then Next: Review, then finally Create user.

Here’s the critical step: Make sure you either download the credentials (Download .csv) or you copy and paste somewhere safe both Access key ID and Secret access key values. We will need them in the next step.

Discourse configuration

Now that you’ve properly set up S3, the final step is to configure your Discourse forum. These instructions should work, but the preferred method is to use environment variables and a CDN as described in Configure an S3 compatible object storage provider for uploads.

Make sure you’re logged in with an administrator account and go the Settings section in the admin panel.

Type in “S3” in the textbox on the right to display only the relevant settings:

You will need to:

  • Check the “enable s3 uploads” checkbox to activate the feature
  • Paste in both “Access Key Id” and “Secret Access Key” in their respective text fields
  • Enter =BUCKET= in the “s3 upload bucket” field

You need to append a prefix to the bucket name if you want to use the same bucket for uploads and backups.

Examples of valid bucket settings
  1. Different buckets

    • s3_upload_bucket: =BUCKET=
    • s3_backup_bucket: =BACKUPS=
  2. Different prefixes

    • s3_upload_bucket: =BUCKET=/uploads
    • s3_backup_bucket: =BUCKET=/backups
  3. Prefix for backups

    • s3_upload_bucket: =BUCKET=
    • s3_backup_bucket: =BUCKET=/backups

The “s3_region” setting is optional and defaults to “US East (N. Virginia)”. You should enter the location (eg. “EU (Frankfurt)”) that is nearest to your users for better performance. If you created the bucket manually, you’ll need to select the region you selected during the creation process.

Enjoy

That’s it. From now on, all your images will be uploaded to and served from S3.

Backups

Do you want store backups of your Discourse forum on S3 as well? Take a look at Configure automatic backups for Discourse.

Frequently Asked Questions

I reused the same bucket for uploads and backups and now backups aren’t working. What should I do?

Name of your bucket for backups

The easiest solution is to append a path to the s3_backup_bucket. Here’s an example of how your settings should look afterwards.

  • s3_upload_bucket: =BACKUPS=
  • s3_backup_bucket: =BACKUPS=/backups

You can use the S3 Console to move existing backups into the new folder.

Do I really need to use separate buckets for uploads and backups?

No, you don’t, but it’s usually the easiest way to set-up. Essentially you need to either use two different buckets or a prefix for the backup bucket. For example, the following combinations will work:

  1. Different buckets

    • s3_upload_bucket: =BUCKET=
    • s3_backup_bucket: =BACKUPS=
  2. Different prefixes

    • s3_upload_bucket: =BUCKET=/uploads
    • s3_backup_bucket: =BUCKET=/backups
  3. Prefix for backups (not recommended unless you previously reused the same bucket – see above question)

    • s3_upload_bucket: =BACKUPS=
    • s3_backup_bucket: =BACKUPS=/backups

I’ve enabled S3 uploads in my Discourse instance (which has been going for a while); what do I do with the existing local uploads?

To migrate your existing uploads to S3, you can do a couple of rake tasks. To perform this, you need SSH access, root permissions, and have entered the discourse app (as per Administrative Bulk Operations). Oh, and you have to set some environmental variables in app.yml. Not for the faint-hearted.

Once you have done all that you are ready for the rake tasks:

rake uploads:migrate_to_s3

Once that is done, posts that need to be rebaked will be marked accordingly and will be rebaked by a regular task. If you have resources and are in a hurry, you can issue this command (which is recommended by the above rake task):

rake posts:rebake_uncooked_posts

Once the posts are all rebaked (and the uploads are working well) you no longer need to include uploads in your backups. And as a bonus, you will be able to Restore a backup from the command line in the event of catastrophe (just keep a copy of app.yml somewhere).

One-way door

Unlike many configuration decisions in Discourse, note that using S3 is a “one-way door;” that is, a move that cannot easily be reversed. There is no safe or maintained way to move files out of S3 to be in the local uploads. In particular, the migrate_to_s3 moves more than just post images to S3; files for which there is no reverse path. (For more details, see Migrate_from_s3 problems).

Backing up your S3 assets

Using versioning, or syncing to a different region are all good strategies.

106 Mi Piace

Questa narrazione deve essere aggiornata. O non posso. Dopo l’installazione, le immagini vengono caricate nel bucket, ma non riesco ad accedervi. Qualunque cosa abbia fatto, non ha funzionato. Qualcuno ha provato di recente e ci è riuscito?

1 Mi Piace

Cosa succede quando provi ad accedervi?

Hai una cdn?

Quale servizio hai utilizzato?

Le immagini sono state caricate e le autorizzazioni del bucket non ti consentono di vederle?

Se il pm deve essere aggiornato, dovrai fornire maggiori informazioni su ciò che hai fatto e su cosa sta succedendo.

Il sito è pubblico? Dove

1 Mi Piace

Seguo esattamente la spiegazione. Carico le immagini. Tuttavia, le immagini non sono accessibili. URL di esempio: https://awssorucevap24.s3.eu-central-1.amazonaws.com/original/2X/6/62c89621b0764112e0029bc91c957dd49d75d819.jpeg

La sezione dei permessi appare così:

Non apro l’ACL quando installo il Bucket, non ci sono informazioni al riguardo e non riesco ad accedervi nemmeno quando modifico questa impostazione nei miei tentativi precedenti.

Nota aggiuntiva: non c’è l’impostazione “Accesso programmatico” quando si crea un utente, c’era molto tempo fa, ora non c’è più. Potrebbe essere correlato a questo? Oppure puoi spiegare come possiamo farlo nel nuovo sistema quando creiamo un utente?

Ho anche preparato il dominio cloudfront e cdn. Il mio unico problema è che non ho accesso ai file. Spero che possiamo trovare quello che mi è sfuggito :slight_smile:

1 Mi Piace

rake uploads:migrate_to_s3
FileStore::ToS3MigrationError: Si prega di fornire le seguenti variabili d’ambiente: (FileStore::ToS3MigrationError)

  • DISCOURSE_S3_BUCKET
  • DISCOURSE_S3_REGION
    e uno tra
  • DISCOURSE_S3_ACCESS_KEY_ID
  • DISCOURSE_S3_SECRET_ACCESS_KEY
    o
  • DISCOURSE_S3_USE_IAM_PROFILE

Per quanto vedo, non è né la chiave di accesso né il profilo IAM. Nel mio caso, sto usando il profilo IAM. Qualche raccomandazione qui?

1 Mi Piace

Penso che tu debba definire questi dati in un file yml. Prima, assicurati che il processo di caricamento funzioni e poi migra.

Comunque, come l’hai configurato? Ho provato a farlo, i file venivano caricati ma non si aprivano nel browser. Dava un errore di accesso negato.

1 Mi Piace

Il caricamento e l’accesso in lettura pubblica funzionano correttamente.

Sto usando il ruolo e la policy IAM associati a EC2. Fammi sapere se desideri maggiori dettagli a riguardo.

Ah, credo di sapere cosa sta succedendo nel tuo caso. Controlla queste configurazioni:

Blocca accesso pubblico (impostazioni del bucket)

Proprietà degli oggetti

Elenco di controllo degli accessi (ACL) – Probabilmente qui sta il trucco per te

3 Mi Piace

Non ho visto nulla nella spiegazione riguardo all’attivazione dell’impostazione ACL, quindi ho provato a farlo senza toccarla ogni volta.

Non è più importante per me, passerò a Cloudflare R2. Questa spiegazione sarà molto utile per qualcun altro in futuro. Grazie.

2 Mi Piace

Non sono sicuro che questa sia la configurazione migliore, ma è così che l’ho trovata finora. Voglio chiedere al @team di dare un’occhiata e guidarci.

È questo il modo migliore per mantenerla sicura e funzionante correttamente?

2 Mi Piace

Potresti condividere maggiori dettagli in merito?

1 Mi Piace

Dopo un po’ di sforzo, sono passato a R2 (non dimentichiamo gli amici che hanno aiutato, rispetto). I file assets e uploads sono pubblicati su clouflare. L’unico problema è che non sono riuscito a caricare automaticamente i file theme-javascripts e stylesheets su R2. Ricercherò questo problema quando avrò tempo.

1 Mi Piace

È stata una decisione tecnica, di costo o entrambe?

1 Mi Piace

Il primo è tecnico, non sono riuscito a installare Amazon, ma stavo già usando Cloudflare, quindi ho pensato perché non installare il CDN da lì. E poi ho visto che è stata una buona decisione perché Cloudflare ha lanciato questo servizio per noi sviluppatori contro gli eccessivamente costosi sistemi cloud S3. Non è molto bello?

1 Mi Piace

È bello. Ci darò un’occhiata più approfondita.

E per quanto riguarda l’integrazione in discourse? Com’è stata quell’esperienza?

Utilizzi anche Cloudflare WAF?

1 Mi Piace

No, non l’ho usato, in realtà non sono sicuro se ne avrò bisogno. Ma uso Cloudflare “ARGO”.

2 Mi Piace

Ho un bucket S3 per caricamenti di immagini con ACL abilitate e un ruolo IAM configurato correttamente e assegnato all’istanza EC2 che esegue la mia istanza standalone. Tuttavia, gli URL delle immagini S3 nei miei post di test non sono visualizzabili. Vorrei ospitare immagini S3 in un bucket che possa essere visualizzato solo dagli utenti Discourse autenticati. Perché i riferimenti URL alle immagini S3 non includono un URL pre-firmato per un accesso corretto? Se accedo al file tramite la console S3 e richiedo un URL pre-firmato tramite AWS per un periodo di tempo definito, come 10 minuti, l’immagine verrà caricata come previsto, quindi so che può funzionare.

1 Mi Piace

Siamo sicuri che debba essere memorizzato nel file app.yml?

L’ho fatto in questo modo;

# entra nell'app
cd /var/discourse
./launcher enter app

# passa le variabili d'ambiente con il comando al runtime
DISCOURSE_S3_BUCKET=my-bucket DISCOURSE_S3_REGION=us-east-1 DISCOURSE_S3_ACCESS_KEY_ID=abcdefg DISCOURSE_S3_SECRET_ACCESS_KEY=zxywqrst rake uploads:migrate_to_s3

L’ho fatto in questo modo poiché non volevo che la mia chiave di accesso rimanesse nel file YAML, dato che ne faccio il backup in vari posti. Passare la chiave segreta AWS attraverso l’ambiente è ~generalmente~ un po’ più “sicuro”. Penso, giusto? Non sono sicuro di quanto duri la cronologia dello scrollback all’interno del container dell’app, immagino che venga cancellata dopo il riavvio del container.

Volevo invece configurare l’accesso S3 nel modo “normale” memorizzando la chiave di accesso AWS e la chiave segreta nel file ~/.aws/credentials del server, tuttavia, non sono del tutto sicuro di come ciò funzionerebbe con l’app in esecuzione all’interno del container.

Inoltre, come indicato nella guida qui, ti viene richiesto di copiare e incollare semplicemente le chiavi di accesso e segrete nell’interfaccia web delle impostazioni di amministrazione di Discourse e salvarle lì; non mi è chiaro dove vengano memorizzate queste chiavi nel backend, poiché salvarle qui non popola il file app.yml con esse. Quindi immagino che siano da qualche parte nel database Postgres? Speriamo crittografate, forse?

Spero che non memorizzando le chiavi di accesso e segrete AWS in app.yml, non avrò problemi in futuro? C’è qualche altro processo che richiede che le chiavi risiedano lì?

1 Mi Piace

Ho abilitato AWS Cloudfront per una cache CDN davanti al mio bucket S3, configurato come descritto qui. Tuttavia, la configurazione di Cloudfront ha richiesto alcune modifiche alle policy di accesso S3. In particolare, sembrano esserci alcune policy che limiterebbero l’accesso al bucket S3 solo a Cloudfront, invece dell’accesso pubblico suggerito da questa guida. Qualcuno è in grado di esaminare quali dovrebbero essere le corrette policy di autorizzazione del bucket S3 se si utilizza la CDN Cloudfront con il proprio bucket S3 su Discourse? Attualmente, la mia CDN funziona, ma non sono sicuro se sia necessario rimuovere eventuali autorizzazioni di accesso superflue dal bucket S3.

1 Mi Piace

Ho usato questa policy del bucket mostrata nel primo post di questo argomento, su un nuovo bucket AWS. Ma oggi, mi dà un errore di sintassi JSON.

Errore nella policy fornita nell'OP/1° post dell'argomento: Dà errori "Missing Principal", "Unsupported Policy" nelle righe 4, 23, 26, 29, e quando riesco a correggere quegli errori in qualche modo leggendo la documentazione AWS, allora appare "Json Syntax Error" verso la fine del codice.
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
               "s3:List*",
               "s3:Get*",
               "s3:AbortMultipartUpload",
               "s3:DeleteObject",
               "s3:PutObject",
               "s3:PutObjectAcl",
               "s3:PutObjectVersionAcl",
               "s3:PutLifecycleConfiguration",
               "s3:CreateBucket",
               "s3:PutBucketCORS"
      ],
      "Resource": [
        "arn:aws:s3:::your-uploads-bucket",
        "arn:aws:s3:::your-uploads-bucket/*"
      ]
    },
    {
       "Effect": "Allow",
       "Action": [
           "s3:ListAllMyBuckets",
           "s3:ListBucket"
       ],
       "Resource": "*"
    }
  ]
}
Quello che ho provato a creare, che non mostra errori di sintassi nella console, tranne un errore "Invalid Principal", che non sono riuscito a superare #### Usato i miei nomi di bucket
{
  "Id": "Policy1725098748430",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1725098741436",
      "Action": [
        "s3:AbortMultipartUpload",
        "s3:CreateBucket",
        "s3:DeleteObject",
        "s3:GetObject",
        "s3:List*",
        "s3:ListBucket",
        "s3:ListBucketMultipartUploads",
        "s3:PutBucketCORS",
        "s3:PutLifecycleConfiguration",
        "s3:PutObject",
        "s3:PutObjectAcl",
        "s3:PutObjectVersionAcl"
      ],
      "Effect": "Allow",
      "Resource": [
	"arn:aws:s3:::t9478010203",
	"arn:aws:s3:::t9478010203/backups/",
	"arn:aws:s3:::t9478010203/uploads/"
	],
      "Principal": {
        "AWS": [
          "123456789012"
        ]
      }
    }
  ]
}
1 Mi Piace

Questo argomento necessita davvero di un aggiornamento: stavo giusto armeggiando con la policy in S3/IAM e ho notato anch’io gli errori. Fortunatamente sembra funzionare bene!

Mi chiedo se qualcuno con le competenze e le conoscenze necessarie potrebbe dare un’occhiata e aggiornare quella che dovrebbe essere la policy. @pfaffman?

5 Mi Piace