hashicorp/terraform · error

error getting bucket: %#v

Error message

error getting bucket: %#v

What it means

In RemoteClient.Put() (client.go:102-104), ossClient.Bucket(c.bucketName) failed. Like error 344, Bucket() is a local name-validation constructor; failure means the bucket name is invalid. This blocks every state write (terraform apply/refresh).

Source

Thrown at internal/backend/remote-state/oss/client.go:104

			if time.Now().Before(deadline) {
				time.Sleep(consistencyRetryPollInterval)
				log.Println("[INFO] retrying OSS RemoteClient.Get...")
				continue
			}

			return nil, diags.Append(fmt.Errorf(errBadChecksumFmt, digest))
		}

		break
	}
	return payload, diags
}

func (c *RemoteClient) Put(data []byte) tfdiags.Diagnostics {
	var diags tfdiags.Diagnostics
	bucket, err := c.ossClient.Bucket(c.bucketName)
	if err != nil {
		return diags.Append(fmt.Errorf("error getting bucket: %#v", err))
	}

	body := bytes.NewReader(data)

	var options []oss.Option
	if c.acl != "" {
		options = append(options, oss.ACL(oss.ACLType(c.acl)))
	}
	options = append(options, oss.ContentType("application/json"))
	if c.serverSideEncryption {
		options = append(options, oss.ServerSideEncryption("AES256"))
	}
	options = append(options, oss.ContentLength(int64(len(data))))

	if body != nil {
		if err := bucket.PutObject(c.stateFile, body, options...); err != nil {
			return diags.Append(fmt.Errorf("failed to upload state %s: %#v", c.stateFile, err))
		}

View on GitHub (pinned to c9def3e214)

Solutions

  1. Correct the bucket name to lowercase letters, digits, and hyphens only (3-63 chars).
  2. Re-run terraform init after fixing the backend block so the client is rebuilt with the valid name.
  3. Check the variable/local feeding bucket for stray characters or empty values.

Example fix

// before
backend "oss" {
  bucket = "TF_State_Bucket"
}

// after
backend "oss" {
  bucket = "tf-state-bucket"
}
Defensive patterns

Strategy: validation

Validate before calling

// Reuse the OSS bucket-name validator from error 344 before any Put path.
if !validBucketName(c.bucketName) {
    return fmt.Errorf("refusing Put: invalid bucket %q", c.bucketName)
}

Try / catch

// Local validation; not retryable. Fix the name and re-init.
if err != nil && strings.Contains(err.Error(), "error getting bucket") {
    return fixBucketName(err)
}

Prevention

When it happens

Trigger: Put() called during state persistence with a bucketName failing OSS naming rules (uppercase, underscore, bad length, illegal chars). No network call is attempted.

Common situations: Bucket attribute was changed to an invalid name after init; interpolation produced an invalid value; copy-paste of a non-normalized bucket name.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/3ccdc8dd8004e45f. Report an issue: GitHub.