"unencrypted_size" is 0 in database. Nextcloud is failing to recalculate, resulting in inconsistent downloads

Nextcloud version (eg, 18.0.2): 18.0.4
Operating system and version (eg, Ubuntu 20.04): Ubuntu 18.04.4 LTS
Apache or nginx version (eg, Apache 2.4.25): Nginx 1.14.0
PHP version (eg, 7.1): PHP 7.3.17

The issue you are facing:
After encrypting files using occ encryption:encrypt all, files struggle to both upload and download. Strangely, the first attempt for the file fails about 3/4ths of the way through, with the browser (Firefox) saying that the download source suddenly became unavailable. No errors appear in the Nextcloud logs. On the second download attempt, Firefox is able to successfully download the file without problems, however, Nextcloud throws the error message that it’s unable to re-calculate the unencrypted filesize. Looking in oc_filecache, all of these files have an unencrypted_filesize value of 0.

What I’ve done to try to resolve the issue:

  1. Decrypt all files, and then reencrypt them. Nextcloud server has no issues while the files are decrypted.
  2. Run occ maintenance:repair. No errors are detected.
  3. Rescan the filesystem. 0 files are found, which I suspect is a side effect of using S3 as internal storage.
  4. Disabling Redis, under the suspicion that it could be a caching problem. No effect.
  5. Banging my head on my keyboard and hoping it resolves itself.

Is this the first time you’ve seen this error? (Y/N): Y

Steps to replicate it:

  1. Setup Nextcloud using S3 as internal storage.
  2. Run occ encryption:encrypt-all.
  3. Download a file using your web browser, or using the Nextcloud sync client. Download initially fails 2/3rd of the way through.
  4. Attempt the download again. Download succeeds, but Nextcloud throws the Bad Signature/Cannot Calculate Unencrypted Size error.

The output of your Nextcloud log in Admin > Logging:

{
  "reqId": "0NLXIv6eziOfUC7pKRCO",
  "level": 3,
  "time": "2020-05-19T00:32:18+00:00",
  "remoteAddr": "2601:1c0:5300:d180:2588:fb84:e283:1c35",
  "user": "ashten",
  "app": "no app in context",
  "method": "GET",
  "url": "/apps/text/session/create?fileId=157276&guestName=null&forceRecreate=false",
  "message": {
    "Exception": "OCP\\Encryption\\Exceptions\\GenericEncryptionException",
    "Message": "Bad Signature",
    "Code": 0,
    "Trace": [
      {
        "file": "/var/www/nextcloud/apps/encryption/lib/Crypto/Crypt.php",
        "line": 468,
        "function": "checkSignature",
        "class": "OCA\\Encryption\\Crypto\\Crypt",
        "type": "->",
        "args": [
          "Yg==",
          null,
          "d4310759b6674dbd03c3ba0930c4a9fade6202d799783e390122788a4e0fd641"
        ]
      },
      {
        "file": "/var/www/nextcloud/apps/encryption/lib/Crypto/Encryption.php",
        "line": 380,
        "function": "symmetricDecryptFileContent",
        "class": "OCA\\Encryption\\Crypto\\Crypt",
        "type": "->",
        "args": [
          "*** sensitive parameter replaced ***",
          null,
          "AES-256-CTR",
          "*** sensitive parameter replaced ***",
          "*** sensitive parameter replaced ***"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Files/Storage/Wrapper/Encryption.php",
        "line": 584,
        "function": "decrypt",
        "class": "OCA\\Encryption\\Crypto\\Encryption",
        "type": "->",
        "args": [
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Files/Storage/Wrapper/Encryption.php",
        "line": 503,
        "function": "fixUnencryptedSize",
        "class": "OC\\Files\\Storage\\Wrapper\\Encryption",
        "type": "->",
        "args": [
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Files/Storage/Wrapper/Encryption.php",
        "line": 167,
        "function": "verifyUnencryptedSize",
        "class": "OC\\Files\\Storage\\Wrapper\\Encryption",
        "type": "->",
        "args": [
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Files/Storage/Wrapper/Encryption.php",
        "line": 404,
        "function": "filesize",
        "class": "OC\\Files\\Storage\\Wrapper\\Encryption",
        "type": "->",
        "args": [
          "*** sensitive parameter replaced ***"
        ]
      },
      {
        "file": "/var/www/nextcloud/apps/terms_of_service/lib/Filesystem/StorageWrapper.php",
        "line": 82,
        "function": "fopen",
        "class": "OC\\Files\\Storage\\Wrapper\\Encryption",
        "type": "->",
        "args": [
          "*** sensitive parameter replaced ***",
          "r"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Files/View.php",
        "line": 1160,
        "function": "fopen",
        "class": "OCA\\TermsOfService\\Filesystem\\StorageWrapper",
        "type": "->",
        "args": [
          "*** sensitive parameter replaced ***",
          "r"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Files/View.php",
        "line": 996,
        "function": "basicOperation",
        "class": "OC\\Files\\View",
        "type": "->",
        "args": [
          "fopen",
          "/ashten/files/Readme.md",
          [
            "read"
          ],
          "r"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Files/Node/File.php",
        "line": 114,
        "function": "fopen",
        "class": "OC\\Files\\View",
        "type": "->",
        "args": [
          "/ashten/files/Readme.md",
          "r"
        ]
      },
      {
        "file": "/var/www/nextcloud/apps/text/lib/Service/DocumentService.php",
        "line": 157,
        "function": "fopen",
        "class": "OC\\Files\\Node\\File",
        "type": "->",
        "args": [
          "r"
        ]
      },
      {
        "file": "/var/www/nextcloud/apps/text/lib/Service/ApiService.php",
        "line": 87,
        "function": "createDocument",
        "class": "OCA\\Text\\Service\\DocumentService",
        "type": "->",
        "args": [
          {
            "__class__": "OC\\Files\\Node\\File"
          }
        ]
      },
      {
        "file": "/var/www/nextcloud/apps/text/lib/Controller/SessionController.php",
        "line": 49,
        "function": "create",
        "class": "OCA\\Text\\Service\\ApiService",
        "type": "->",
        "args": [
          157276,
          null,
          null,
          null,
          false
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/AppFramework/Http/Dispatcher.php",
        "line": 170,
        "function": "create",
        "class": "OCA\\Text\\Controller\\SessionController",
        "type": "->",
        "args": [
          157276,
          null,
          false
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/AppFramework/Http/Dispatcher.php",
        "line": 99,
        "function": "executeController",
        "class": "OC\\AppFramework\\Http\\Dispatcher",
        "type": "->",
        "args": [
          {
            "__class__": "OCA\\Text\\Controller\\SessionController"
          },
          "create"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/AppFramework/App.php",
        "line": 125,
        "function": "dispatch",
        "class": "OC\\AppFramework\\Http\\Dispatcher",
        "type": "->",
        "args": [
          {
            "__class__": "OCA\\Text\\Controller\\SessionController"
          },
          "create"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/AppFramework/Routing/RouteActionHandler.php",
        "line": 47,
        "function": "main",
        "class": "OC\\AppFramework\\App",
        "type": "::",
        "args": [
          "OCA\\Text\\Controller\\SessionController",
          "create",
          {
            "__class__": "OC\\AppFramework\\DependencyInjection\\DIContainer"
          },
          {
            "_route": "text.Session.create"
          }
        ]
      },
      {
        "function": "__invoke",
        "class": "OC\\AppFramework\\Routing\\RouteActionHandler",
        "type": "->",
        "args": [
          {
            "_route": "text.Session.create"
          }
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Route/Router.php",
        "line": 299,
        "function": "call_user_func",
        "args": [
          {
            "__class__": "OC\\AppFramework\\Routing\\RouteActionHandler"
          },
          {
            "_route": "text.Session.create"
          }
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/base.php",
        "line": 1008,
        "function": "match",
        "class": "OC\\Route\\Router",
        "type": "->",
        "args": [
          "/apps/text/session/create"
        ]
      },
      {
        "file": "/var/www/nextcloud/index.php",
        "line": 38,
        "function": "handleRequest",
        "class": "OC",
        "type": "::",
        "args": []
      }
    ],
    "File": "/var/www/nextcloud/apps/encryption/lib/Crypto/Crypt.php",
    "Line": 492,
    "Hint": "Bad Signature",
    "CustomMessage": "--"
  },
  "userAgent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0",
  "version": "18.0.4.2"
}

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

<?php
$CONFIG = array (
  'instanceid' => '<Hidden>',
  'passwordsalt' => '<Hidden>',
  'secret' => '<Hidden>',
  'trusted_domains' => 
  array (
    0 => 'storage.platypus-sandbox.com',
  ),
  'objectstore' => 
  array (
    'class' => '\\OC\\Files\\ObjectStore\\S3',
    'arguments' => 
    array (
      'bucket' => '<Hidden>',
      'autocreate' => true,
      'key' => '<Hidden>,
      'secret' => '<Hidden>',
      'hostname' => 'sfo2.digitaloceanspaces.com',
      'port' => 443,
      'use_ssl' => true,
      'region' => 'sfo2',
    ),
  ),
  'datadirectory' => '/var/www/nextcloud/data',
  'dbtype' => 'mysql',
  'version' => '18.0.4.2',
  'overwrite.cli.url' => 'https://storage.platypus-sandbox.com',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => '<Hidden>',
  'dbpassword' => '<Hidden>',
  'installed' => true,
  'mail_smtpmode' => 'smtp',
  'mail_smtpsecure' => 'tls',
  'mail_sendmailmode' => 'smtp',
  'mail_from_address' => 'storage',
  'mail_domain' => 'platypus-sandbox.com',
  'mail_smtphost' => 'platypus-sandbox.com',
  'mail_smtpport' => '587',
  'mail_smtpauth' => 1,
  'mail_smtpname' => '<Hidden>@platypus-sandbox.com',
  'mail_smtppassword' => '<Hidden>',
  'mail_smtpauthtype' => 'LOGIN',
  'ldapIgnoreNamingRules' => false,
  'ldapProviderFactory' => 'OCA\\User_LDAP\\LDAPProviderFactory',
  'maintenance' => false,
  'memcache.local' => '\\OC\\Memcache\\Redis',
  'memcache.distributed' => '\\OC\\Memcache\\Redis',
  'redis' => 
  array (
    'host' => '/var/run/redis/redis-server.sock',
    'port' => 0,
  ),
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'enable_previews' => false,
);

The output of your Apache/nginx/system log in /var/log/____: (Note, the nginx error log was looking sparse. The below error may or may not be relevant.)

2020/05/18 20:27:56 [error] 22355#22355: *467 access forbidden by rule, client: <IP Address Hidden>, server: storage.platypus-sandbox.com, request: "GET /data/.ocdata?t=1589848074373 HTTP/1.1", host: "storage.platypus-sandbox.com"