# שינוי דלי s3 להעלאות

**URL:** <https://meta.discourse.org/t/changing-s3-bucket-for-uploads/188553>\
**Category:** Product Support\
**Created:** [29 באפריל,‏ 2021,‏ 2:12pm UTC](https://meta.discourse.org/t/changing-s3-bucket-for-uploads/188553 "2021-04-29T14:12:22Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [30 באפריל,‏ 2021,‏ 4:05pm UTC](https://meta.discourse.org/t/changing-s3-bucket-for-uploads/188553/6 "2021-04-30T16:05:22Z")

</div>

Hey @Jite !

See if this works for you. If it does, I’ll go about creating a proper #howto

### Old buckets

This assumes that you can install and configure a tool to move your data from your old bucket to a local machine and then again do the same from local to the new bucket. See [aws cli sync](https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html) (which can be configured for non-AWS buckets) and [gsutil rsync](https://download.huihoo.com/google/gdgdevkit/DVD1/developers.google.com/storage/docs/gsutil/commands/rsync.html) for information. If you have huge amounts of data or are moving between buckets on the same provider, then you might want to investigate methods that move the data directly between buckets.

Get in a directory suitable for a holding space. (e.g., `mkdir temp-bucket; cd temp-bucket`) before doing something like the following. These examples include the `-n` and `--dry-run` switches to show you what will happen. If that looks like what you want, run the command again without that switch.

### Move old data from old bucket to to local

```plaintext
    gsutil rsync -r -n gs://=OLD= .

```

or

```plaintext
    aws s3 sync s3://=OLD= .

```

### Move data from local to new bucket

```plaintext
    gsutil rsync -r -n . gs://=NEW=

```

or

```plaintext
    aws s3 sync . s3://=NEW=

```

### Updating the database to use the new bucket

You’’ do these commands at the Rails console, to get there, you’ll do a

```
cd /var/discourse
./launcher enter app
rails c

```

For the new bucket, upload an image with the new configuration and do this:

```plaintext
Upload.last.url

```

You should see something like

```
=> "//discourse-bucket.s3.dualstack.us-east-2.amazonaws.com/`original/2X/7/12345fbea574afc4e02db80107e6682430aede2c.png"

```

You’d then get `discourse-bucket.s3.dualstack.us-east-2.amazonaws.com` for the new bucket. Get the old bucket hostname similarly from the above.

Use this to check that your uploads are where you think they are:

```plaintext
Upload.order(Arel.sql('RANDOM()')).limit(10).pluck(:id, :url)

```

Now, you’ll update the database to use the new bucket rather than the old one. `DbHelper.remap` will replace occurrences in all tables.

```plaintext
DbHelper.remap("//=OLDHOST=/","//=NEWHOST=/")

```

### Moving to AWS might require clearing your `s3_endpoint`.

NOTE: If you have a `s3_endpoint` defined in your SiteSettings in the database and switch to AWS (where no endpoint is needed), then you’ll need to clear that site setting after you build the new container with the updated settings (or after you restore a database that has it set).

### Rebake posts that refer to bucket rather than S3 CDN

If you have posts that link directly to the new s3 bucket (perhaps you didn’t have an `s3_cdn_url` defined before), then here’s how to rebake only the posts that need it.

Get the posts:

```
  posts=Post.where("cooked like '%=NEWHOST=%'")

```

See how many:

```
  posts.count

```

Rebake those posts:

```
  posts.each do |p| p.rebake! end

```

Or, just replace the bucket with the cdn:

```plaintext
posts.each do |p|
  p.cooked.gsub!(/=NEWHOST=/,"=CDN=")
  p.save!
end

```

---

_[View the full topic](https://meta.discourse.org/t/changing-s3-bucket-for-uploads/188553)._
